的onClick不工作在OnCreate中 [英] onClick not working In OnCreate
本文介绍了的onClick不工作在OnCreate中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我有一个2个按钮的布局
So I have a 2 buttons in a layout
像这样:
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:onClick="onClick">
<Button
android:id="@+id/btnEdit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Edit Spot" />
<Button android:id="@+id/btnGo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Google Spot"
android:layout_weight="1"/>
</LinearLayout>
然后在OnCreate为这个活动我有:
Then in the onCreate for the activity for this I have:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_spot);
// save button
ButtonEdit = (Button)findViewById(R.id.btnEdit);
ButtonGo = (Button) findViewById(R.id.btnGo);
// getting product details from intent
Intent i = getIntent();
// getting product id (pid) from intent
pid = i.getStringExtra(TAG_PID);
// Getting complete product details in background thread
new GetProductDetails().execute();
// save button click event
ButtonEdit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(), "Edit button pressed", Toast.LENGTH_LONG).show();
}
});
// Go button click event
ButtonGo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(), "Go button pressed!", Toast.LENGTH_LONG).show();
}
});
}
但由于某些原因,当我点击的按钮没有任何反应。我在别人问这个问题左顾右盼,试图这些,但仍然一无所获。是否有人可以帮助我弄清楚为什么它不工作?
But for some reason when I tap the buttons nothing happens. I've looked around at others asking the question and tried those but still nothing. Can someone please help me figure out why it isn't working?
感谢您,
泰勒
Thank you, Tyler
推荐答案
您必须在添加
的定义,而不是在的onClick
属性按钮的LinearLayout
。
即使你想重用名为的onClick
您可以设置每个按键的标记,并做了开关$ C相同的方法$ C>为每个标签。例如:
Even if you want to reuse the same method called onClick
you can set a tag for each button, and do a switch
for each tag. For instance:
在您的布局:
android:tag="1"
在您的code:
public void onClick(View v) {
String tag = (String) v.getTag();
switch (Integer.parseInt(tag)) {
case 1: // First button
...
break;
case 2: // Second button
...
break;
}
}
这篇关于的onClick不工作在OnCreate中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文