我有一个按钮,当我点击一个对话框出现。问题是,setOnClickListener
没有响应我的按钮。按钮OnClickListener不工作
这里是我的Java代码:
Button b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(usage);
b2 = (Button) findViewById(R.id.button2);
b2.setOnClickListener(price);
b3 = (Button) findViewById(R.id.button3);
b3.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
System.out.println("Button3");
mainDialog3 = new Dialog(Advice.this);
mainDialog3.setContentView(R.layout.fuel);
mainDialog3.setCancelable(true);
mainDialog3.show();
Window window = mainDialog3.getWindow();
window.setLayout(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
}
});
private View.OnClickListener usage = new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
System.out.println("Button1");
Toast.makeText(getApplicationContext(), "Button 1",
Toast.LENGTH_LONG).show();
mainDialog1 = new Dialog(Advice.this);
mainDialog1.setContentView(R.layout.usage);
mainDialog1.setCancelable(true);
mainDialog1.show();
Window window = mainDialog1.getWindow();
window.setLayout(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
}
};
private View.OnClickListener price = new View.OnClickListener() {
public void onClick(View v) {
System.out.println("Button2");
mainDialog2 = new Dialog(Advice.this);
mainDialog2.setContentView(R.layout.price);
mainDialog2.setCancelable(true);
mainDialog2.show();
Window window = mainDialog2.getWindow();
window.setLayout(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
}
};
问题是与按钮1(b1
)。其余的按钮(b2
,b3
)工作得很好。下面是我的XML:
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/button7"
android:text="Edit" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button3"
android:layout_alignLeft="@+id/button1"
android:text="Edit" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView8"
android:layout_alignLeft="@+id/button2"
android:text="Edit" />
愚蠢的问题,但你确定你点击的按钮是button1?好像你在RelativeLayout中有多个按钮都具有相同的文本(“编辑”)。是否有可能另一个按钮位于你认为是button1的顶部,从而阻止它被点击? – brianestey
是的,我检查没有其他按钮重叠按钮。相反,我添加了一个新的按钮,并检查,但仍然是同样的问题。 – Droid
嘿,非常感谢你,我不知道问题在哪里,但它现在起作用了。 – Droid