gpt4 book ai didi

java - 安卓工作室 : ImageButton's not activating when tapped

转载 作者:行者123 更新时间:2023-11-29 19:41:51 27 4
gpt4 key购买 nike

我正在开发此应用程序并正在对其进行测试,但我不太确定哪里出了问题。当我点击或按住面板按钮时,绝对没有任何反应。 logcat 中唯一显示的是触摸事件。我认为我的格式可能有问题,但由于这是我第一次使用 Java(过去只使用过微处理器类的程序集)我不确定它可能是什么。

编辑:根据建议删除了很多与问题无关的额外代码

这是MainActivity.java

 public class MainActivity extends AppCompatActivity {
int i=0;
int VOA=0;
int VOB=0;
int [] c ={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30};



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


// The Draw Control for the Deck. It will only draw if it is in it's 0 state
ImageButton Deck =(ImageButton)(findViewById(R.id.Deck));
assert Deck != null;
Deck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if( VOA==0){
VOA=c[i];
i=i+1;


} else if (VOB==0){
VOB=c[i];
i=i+1;

}

}
});

// Uses Collections shuffle to shuffle c
Deck.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
List c= new ArrayList();
Collections.shuffle(c);


return false;
}});


//Discard for Objective A by setting it to 0 and thus updating it's case statement.
ImageButton ObjectiveA =(ImageButton)(findViewById(R.id.ObjectiveA));
assert ObjectiveA != null;
ObjectiveA.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
VOA=0;
return false;
}
});



//Discard for Objective B by setting it to 0 and thus updating it's case statement.
ImageButton ObjectiveB =(ImageButton) (findViewById(R.id.ObjectiveB));
assert ObjectiveB != null;
ObjectiveB.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
VOB=0;
return false;

}
});

}
}
}

这也是activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="gallostsp.darkagedeck.MainActivity"
android:background="@drawable/background"
android:screenOrientation="landscape">



<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Deck"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/VerticalCenterLine"
android:layout_toStartOf="@+id/VerticalCenterLine"
android:background="@drawable/deck"
android:contentDescription="@string/objectives_deck"
android:clickable="true"
android:longClickable="true" />

<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ObjectiveA"
android:background="@drawable/objectivearea"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_toRightOf="@+id/VerticalCenterLine"
android:layout_toEndOf="@+id/VerticalCenterLine"
android:layout_above="@+id/ObjectiveB"
android:contentDescription="@string/objective_a"
android:longClickable="true"
android:clickable="false" />

<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ObjectiveB"
android:layout_below="@+id/HorizontalCenterLine"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:background="@drawable/objectivearea"
android:layout_toRightOf="@+id/VerticalCenterLine"
android:layout_toEndOf="@+id/VerticalCenterLine"
android:layout_alignParentBottom="true"
android:contentDescription="@string/objective_b"
android:longClickable="true"
android:clickable="false" />


</RelativeLayout>

最佳答案

如果您使用了 longclickListener(),则返回 true

   Deck.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
List c= new ArrayList();
Collections.shuffle(c);


return true; // change to true
}});

关于java - 安卓工作室 : ImageButton's not activating when tapped,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38531554/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com