Android onClick在OnCreate中不工作
所以我在布局中有两个按钮 像这样:Android onClick在OnCreate中不工作,android,button,onclick,Android,Button,Onclick,所以我在布局中有两个按钮 像这样: <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:onClick="onClick"> <Button android:id="@+id/btnEdit" android:layout_wid
<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>
但由于某种原因,当我点击按钮时,什么也没发生。我环顾四周,问了其他人这个问题,并尝试了这些,但仍然一无所获。有人能帮我弄清楚为什么它不起作用吗
谢谢,,
Tyler您必须在
按钮的定义中,而不是在线性布局中,添加onClick
属性
即使要重复使用名为onClick
的相同方法,也可以为每个按钮设置标记,并为每个标记执行切换。例如:
在布局中:
android:tag="1"
在代码中:
public void onClick(View v) {
String tag = (String) v.getTag();
switch (Integer.parseInt(tag)) {
case 1: // First button
...
break;
case 2: // Second button
...
break;
}
}
请使您的按钮可点击:
<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"
android:clickable:true />
<Button android:id="@+id/btnGo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Google Spot"
android:layout_weight="1"
android:clickable:true/>
</LinearLayout>
我希望这会有所帮助。从线性布局中删除onClick
。现在您有嵌套的onClickListeners
。LinearLayout正在拦截事件,可能不会将其传递下去
编辑:出于某种原因,有人盲目地否决了所有这些答案。发布一条注释,解释为什么这是错误的。我放置了两个按钮的onClick属性,但现在它崩溃了,原因是:01-16 13:24:00.728:E/AndroidRuntime(15625):java.lang.IllegalStateException:找不到onClick(视图)方法在id为“btnEdit”的视图类android.widget.Button上的活动类com.example.androidhive.viewspottactivity for onClick handler中,您必须将onClick
方法放在onCreate
方法之外,但放在活动内部才能使其工作。顺便说一下。。。我不知道为什么我的答案被否决了,它没有做任何不好的练习(AFAIK),而且有效。我没有否决,我的意思是你确实帮助了我。很抱歉你被否决了,但谢谢你的帮助。没问题,我不是说是你,我只是说。希望有帮助!我对你的答案投了赞成票,因为我和你对盲目投反对票的看法相同,你的答案是正确的。恐怕也有人对我的答案投了反对票--我不是盲目投反对票。萨尔曼·阿尤布的回答是错误的,因为问题出在线性布局上。这是无用的。2) 按钮是可点击的:默认为true,加上你在那里输入了一个错误。NKN的方法在标签方面有点糟糕。最好使用id。@doctordrive那么我的怎么了?默认情况下,按钮是可单击的,但如果父级按钮也是可单击的,则除非您告诉它,否则不会传递单击事件。例如,将按钮放在EditText中会导致问题。仍然没有任何问题,我尝试将android:onClick=“onClick”放在按钮属性中,而不是布局中,结果出现错误:01-16 13:40:29.540:E/AndroidRuntime(17174):java.lang.IllegalStateException:找不到方法onClick(视图)在id为“btnEdit”的视图类android.widget.Button上的onClick处理程序的活动类com.example.androidhive.viewspottactivity中,您没有正确应用答案。查看上面的代码,您没有实现public void onClick(视图v){//您的实现}
方法。我没有投反对票,我的意思是你确实帮助了我。很抱歉你投了反对票,但谢谢你的帮助。好吧,所以我有点困惑,如果你不能说我对android编程相当陌生的话。我会为//your实现投什么票?onCreate中的onclick不应该实现我想要的吗o?android:onClick=“onClick”应该在布局内部还是在按钮属性内部?正如您在线性布局中明确提到的onClick()
方法,那么您需要单独实现它(与上面答案中定义的onCreate…)方法分开。您已经使用了onClick()
按钮上的侦听器,它将仅处理这些按钮的单击事件。
<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"
android:clickable:true />
<Button android:id="@+id/btnGo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Google Spot"
android:layout_weight="1"
android:clickable:true/>
</LinearLayout>
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();
}
});
}
public void onCLick(View v)
{
//Your Implementation
}