Java Button setOnClickListener不能只为一个按钮工作
我的活动中有两个不同的按钮,还有一个if语句来显示它们 我的if语句运行良好,并显示了正确的按钮,但当我单击其中一个按钮时,它没有任何作用!(实际上,它根本不可点击!) 问题是,它不会返回任何错误或日志,以了解问题所在!这是日志我禁用了按钮。。。(我没有) 我检查了是否是声明,但所有的声明都很好 我检查了XML,也没有问题:Java Button setOnClickListener不能只为一个按钮工作,java,android,button,onclicklistener,Java,Android,Button,Onclicklistener,我的活动中有两个不同的按钮,还有一个if语句来显示它们 我的if语句运行良好,并显示了正确的按钮,但当我单击其中一个按钮时,它没有任何作用!(实际上,它根本不可点击!) 问题是,它不会返回任何错误或日志,以了解问题所在!这是日志我禁用了按钮。。。(我没有) 我检查了是否是声明,但所有的声明都很好 我检查了XML,也没有问题: <LinearLayout android:id="@+id/linearLayoutImportant" android:layout_width
<LinearLayout
android:id="@+id/linearLayoutImportant"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="50dp"
android:layout_centerInParent="true"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical"
android:visibility="gone" >
<TextView
android:id="@+id/textViewImportant"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:text="@string/important"
android:textSize="20sp"
android:textColor="#FFFFFF"
android:textStyle="bold" />
<Button
android:id="@+id/buttonImportant"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="@string/details" />
</LinearLayout>
...
...
<LinearLayout
android:id="@+id/linearLayoutNormal"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginTop="25dp"
android:orientation="vertical"
android:visibility="gone" >
<Button
android:id="@+id/buttonClose"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_gravity="end"
android:background="@android:drawable/ic_menu_close_clear_cancel" />
<TextView
android:id="@+id/textViewNormal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_gravity="center_horizontal"
android:gravity="center_vertical|center_horizontal"
android:text="@string/normal"
android:textStyle="bold" />
<Button
android:id="@+id/buttonNormal"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_margin="2dp"
android:layout_gravity="center_horizontal"
android:text="@string/details" />
</LinearLayout>
编辑2:
好吧,伙计们,也许我没说清楚
isNeeded
是一个变量,我从另一个地方控制它
当我将其设置为false时,第二个if
将得到很好的执行,当我将其设置为true时,第一个if
也将执行,但只有按钮不可单击
编辑3:
我还选中了.setEnabled(true)
或。可设置可点击(true)代码>,仍不工作…:/
解决了的:
我使用HierarchyViewer发现了问题。问题出在XML中。。。我在第二个按钮的顶部有一个列表视图,但它是空的,所以我认为什么都没有
对不起,各位…请尝试不使用if语句,以了解是否存在验证问题,并且您只设置了一个侦听器布尔变量是必需的
只能取两个值中的一个,true或false。因此,只执行一个if条件
由于只有一个条件为真,因此您只需将onClickListener
设置为其中一个按钮,因此无法单击另一个按钮,并且onClick
方法中的日志消息在日志中不可见
希望这能回答你的问题。
祝您一切顺利:)在之前设置isNeeded=true
if (isNeeded)
{
// buttonImportant is not clickable
buttonImportant.setOnClickListener(new View.OnClickListener() {
然后再测试一次。
祝你好运 发布我的ans时出错,因此我已截图。请复习一下你的答案
我使用HierarchyViewer发现了问题。问题出在XML中。。。我在第二个按钮的顶部有一个列表视图,但它是空的,所以我认为什么都没有
对不起,各位…您是否在AndroidManifest.xml中添加了DetailsActivity
活动?
这两个按钮中的哪一个不适用于您?您只设置了按钮的一键式侦听器显示您在活动中声明按钮的代码2线性布局的容器是什么?它们是否包含在框架布局中?linearLayoutImportant和linearLayoutNormal是否相互重叠?它确实有效,但我不希望它自己执行单击!用户应按下一个按钮。。。(顺便说一下,按钮仍然无法点击)请发布实际代码而不是截图。详情请参阅。谢谢,是的,我知道。。在上传图片之前,我已经评论过在上传我的帖子时出现了一个问题,因为我不得不上传图片
LinearLayout linearLayoutImportant, linearLayoutNormal;
Button buttonImportant, buttonClose, buttonNormal;
...
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearLayoutImportant = (LinearLayout) findViewById(R.id.linearLayoutImportant );
buttonImportant = (Button) findViewById(R.id.buttonImportant);
buttonClose = (Button) findViewById(R.id.buttonClose);
buttonClose .setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
linearLayoutNormal.setVisibility(View.GONE);
}
});
buttonNormal = (Button) findViewById(R.id.buttonNormal);
linearLayoutNormal = (LinearLayout) findViewById(R.id.linearLayoutNormal);
}
if (isNeeded)
{
// buttonImportant is not clickable
buttonImportant.setOnClickListener(new View.OnClickListener() {