Android 按钮不可点击
我有一个按钮“saveStats”,似乎不接受点击 在模拟器上没有单击的动画 我尝试过清理并重新启动Eclipse,但没有成功 我也有复选框小部件,工作良好,只有按钮不工作。这是我的密码: 谢谢你的帮助:) 按钮XML代码:Android 按钮不可点击,android,android-layout,android-button,Android,Android Layout,Android Button,我有一个按钮“saveStats”,似乎不接受点击 在模拟器上没有单击的动画 我尝试过清理并重新启动Eclipse,但没有成功 我也有复选框小部件,工作良好,只有按钮不工作。这是我的密码: 谢谢你的帮助:) 按钮XML代码: <Button android:id="@+id/saveStats" android:layout_width="wrap_content" android:layout_height="wrap_content"
<Button
android:id="@+id/saveStats"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:text="save" />
您必须以这种方式实现
OnClickListener()
public class ActivityName extends Activity implements OnClickListener
{
然后覆盖你的onClick()
只是一张纸条。另一种方法是在xml中为每个按钮声明相同的click函数,然后在java代码中编写该函数。这可以防止您需要实现OnClickListener
并在代码中声明按钮和设置OnClickListener()
。你怎么做取决于你最喜欢什么,但这只是另一种选择。这里有一个小例子:
在xml中为每个按钮声明一个函数
<Button
android:id="@+id/btn1"
...
android:onClick="functionName"/>
<Button
android:id="@+id/btn2"
...
android:onClick="functionName"/>
该方法只需要是公共的
,接受视图
作为其唯一的参数
,并且与您在xml
中声明的名称相同,您没有使用break语句。确保您的类实现OnClickListener
@Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.saveStats :
Toast.makeText(ActivityName.this, "Button clicked", Toast.LENGTH_LONG).show();
break;
case R.id.checkBoxAge :
//dosomething
break;
}
}
要显示toast,请使用活动上下文。要知道为什么要在下面的链接中通过Commonware检查答案
范例
activity_main.xml
<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"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView1"
android:layout_alignRight="@+id/textView1"
android:layout_marginBottom="66dp"
android:layout_marginRight="35dp"
android:text="Button1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button1"
android:text="Button2" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button2"
android:layout_alignParentLeft="true"
android:layout_marginBottom="18dp"
android:text="CheckBox" />
</RelativeLayout>
我做了超控,但它不起作用。为什么按钮不起作用而复选框不起作用?您需要将return
更改为break
。Return用于退出方法并返回onClick()
中的void
值<代码>中断
用于退出开关
。即使这确实有效,如果您决定在方法中添加行为,无论单击了哪个视图
,代码都不会运行return完成方法,因此开关也会结束。我在返回之前没有收到代码。请更改代码,看看我们是否正确。这将花费你3秒钟的时间。不正确。我通过改变按钮在线性布局中的位置来修复它,它是如何工作的。我不需要中断,我强制返回。你不返回,因为onClick方法返回类型是void@tomer使用break语句并使用活动上下文代替它来显示toastR.id.saveStats未被单击。“我看不到祝酒词。@如果您使用break语句,我想应该是这样。”
public void functionName(View v)
{
... switch on your id just like you would the other way
}
@Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.saveStats :
Toast.makeText(ActivityName.this, "Button clicked", Toast.LENGTH_LONG).show();
break;
case R.id.checkBoxAge :
//dosomething
break;
}
}
<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"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView1"
android:layout_alignRight="@+id/textView1"
android:layout_marginBottom="66dp"
android:layout_marginRight="35dp"
android:text="Button1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button1"
android:text="Button2" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button2"
android:layout_alignParentLeft="true"
android:layout_marginBottom="18dp"
android:text="CheckBox" />
</RelativeLayout>
public class MainActivity extends Activity implements OnClickListener{
CheckBox cb ;
Button b,b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b= (Button) findViewById(R.id.button1);
b1= (Button) findViewById(R.id.button2);
cb = (CheckBox) findViewById(R.id.checkBox1);
cb.setOnClickListener(this);
b.setOnClickListener(this);
b1.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.button1 :
Toast.makeText(MainActivity.this, "Button clicked1", Toast.LENGTH_LONG).show();
break;
case R.id.button2 :
Toast.makeText(MainActivity.this, "Button clicked2", Toast.LENGTH_LONG).show();
break;
case R.id.checkBox1:
if(cb.isChecked())
Toast.makeText(MainActivity.this, "check box clicked", Toast.LENGTH_LONG).show();
break;
}
}
}