Android 在viewgroup中以编程方式禁用触摸事件
我有一个应用程序,其中我将一些图像设置为固定角度。在270到90角度之间,我使用“不可见”隐藏图像。但其触摸事件仍在工作。显然,因为图像存在。我希望以编程方式在这些角度禁用触摸事件。有人能指导我如何实现这一点吗 这是我在onLayout中的代码-Android 在viewgroup中以编程方式禁用触摸事件,android,touch-event,viewgroup,Android,Touch Event,Viewgroup,我有一个应用程序,其中我将一些图像设置为固定角度。在270到90角度之间,我使用“不可见”隐藏图像。但其触摸事件仍在工作。显然,因为图像存在。我希望以编程方式在这些角度禁用触摸事件。有人能指导我如何实现这一点吗 这是我在onLayout中的代码- float angleDelay = 360 / getChildCount(); if (!childRotate) { for (Integer i = 0; i < childCount; i++) {
float angleDelay = 360 / getChildCount();
if (!childRotate) {
for (Integer i = 0; i < childCount; i++) {
final Left_Unit textName = (Left_Unit) getChildAt(i);
if (textName.getVisibility() == GONE) {
continue;
}
if (angle > 360) {
angle -= 360;
} else {
if (angle < 0) {
angle += 360;
}
}
textName.setAngle(angle);
textName.setPosition(i);
if (position == name.size()) {
position = 0;
}
if (position < childCount) {
// textName.setVisibility(View.VISIBLE);
textName.setTextname(name.get(position));
textName.setText(name.get(position));
position++;
}
if (angle <= 270 && angle >= 90) {
textName.setVisibility(View.VISIBLE);
}
float angleDelay=360/getChildCount();
如果(!childRotate){
for(整数i=0;i360){
角度-=360;
}否则{
如果(角度<0){
角度+=360;
}
}
textName.setAngle(角度);
textName.setPosition(i);
if(position==name.size()){
位置=0;
}
如果(位置<儿童计数){
//textName.setVisibility(View.VISIBLE);
textName.setTextname(name.get(position));
textName.setText(name.get(position));
位置++;
}
如果(角度=90){
textName.setVisibility(View.VISIBLE);
}
它很好用
对于旋转,我调用了这个方法
for (Integer i = 0; i < childCount; i++) {
if (angle > 360) {
angle -= 360;
} else {
if (angle < 0) {
angle += 360;
}
}
final Left_Unit child = (Left_Unit) getChildAt(i);
if (child.getVisibility() == GONE) {
continue;
}
if (position == name.size()) {
position = 0;
}
if (angle > 85 && angle < 90) {
// child.setVisibility(View.VISIBLE);
child.setTextname(name.get(position));
child.setText(name.get(position));
position++;
}
if (angle <= 270 && angle >= 90) {
child.setVisibility(View.VISIBLE);
} else {
child.setVisibility(View.GONE);//when i use View.INVISIBLE it works fine & images become visible after rotation but with gone it's not visible again
}
for(整数i=0;i360){
角度-=360;
}否则{
如果(角度<0){
角度+=360;
}
}
最终左单元子对象=(左单元)getChildAt(i);
if(child.getVisibility()==GONE){
继续;
}
if(position==name.size()){
位置=0;
}
如果(角度>85和角度<90){
//setVisibility(View.VISIBLE);
setTextname(name.get(position));
setText(name.get(position));
位置++;
}
如果(角度=90){
setVisibility(View.VISIBLE);
}否则{
child.setVisibility(View.GONE);//当我使用View.INVISIBLE时,它工作得很好&图像在旋转后变为可见,但随着它的消失,它就不再可见了
}
这是我的xml
<com.example.converter.view.Left_Unit
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="a1"
android:textColor="#ffffff"
android:visibility="invisible"
left:textname="text1" />
使用if(angle>=270&&angle
if(angle>=270&&angle首先需要通过编程检查视图是否可见。使用此代码检查可见性
image.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (v.getVisibility() == View.VISIBLE) {
// Its visible
} else {
do nothing
}
}
});
您使用什么语句来隐藏imageif(angle>=270&&angle问题与ontouch有关,而与可见性无关。我也面临同样的问题,但如果使用view.INVISIBLE隐藏图像,它将隐藏图像,但仍处于触摸模式,但如果使用view。Goe将隐藏图像,并在触摸模式下禁用。它解决了我的问题实际上在xml中,我将可见性设置为INVISIBLE。因此,当我使用if(angle>=270&&angle What时,我认为您的代码存在其他一些问题,因为我使用了View.Gone和View.Visible多次,但从未遇到任何问题,因此请共享您的代码,这将帮助我理解问题OK glb多做一件事删除android:visibility=“invisible”并使用setvisibility视图。在要隐藏图像的类文件中。是的,我知道&这是我想知道的。如何在Visibility消失时禁用ontouch。这就是为什么我编写了do nothing,,,,,将else保留为空
image.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if(!(angle >=270 && angle <=90)){
//handle click
}
}
}
image.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (v.getVisibility() == View.VISIBLE) {
// Its visible
} else {
do nothing
}
}
});