[英]Android XML layout checkbox “onclick” method bind to a function not found in MainActivity
Is it possible in Android to bind the "onclick" method from a checkbox to a function defined in a java class file directly in the XML file? Is it possible in Android to bind the "onclick" method from a checkbox to a function defined in a java class file directly in the XML file?
something like this像这样的东西
//XML layout File
....
<CheckBox
android:id="@+id/checkbox_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/testString"
android:textColor="@color/testcolor"
android:onClick="onClickCheckbox_1_do"/>
...
public class TestClass{
...
public void onClickCheckbox_1_do(View view) {
//DoStuff
}
}
Thanks!谢谢!
Yes, you can.是的你可以。 I just did.
我已经做了。 Write this code in your layout file:
在你的布局文件中写下这段代码:
<CheckBox
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="click"/>
And in your activity:在您的活动中:
public void click(View view)
{
}
Yes.是的。 it's possible.
这是可能的。
To define the click event handler for a checkbox, you should add android:onClick attribute to the element in your XML layout.要为复选框定义单击事件处理程序,您应该将 android:onClick 属性添加到 XML 布局中的元素。 The value for this attribute must be the name of the method you want to call in response to a click event.
此属性的值必须是您要调用以响应单击事件的方法的名称。 The Activity hosting the layout must then implement the corresponding method.
托管布局的 Activity 必须实现相应的方法。
The method you declare in the android:onClick attribute must have a signature exactly as shown above.您在 android:onClick 属性中声明的方法必须具有完全如上所示的签名。 Specifically, the method must: 1. Be public 2. Return void, and 3. Define a View as its only parameter (this will be the View that was clicked)
具体来说,该方法必须: 1. 公开 2. 返回 void,以及 3. 将 View 定义为其唯一参数(这将是被点击的 View)
Below is the code examples: xml.下面是代码示例:xml。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<CheckBox android:id="@+id/checkbox_meat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/meat"
android:onClick="onCheckboxClicked"/>
<CheckBox android:id="@+id/checkbox_cheese"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cheese"
android:onClick="onCheckboxClicked"/>
</LinearLayout>
The activity method should look as follows:活动方法应如下所示:
public void onCheckboxClicked(View view) {
// Is the view now checked?
boolean checked = ((CheckBox) view).isChecked();
// Check which checkbox was clicked
switch(view.getId()) {
case R.id.checkbox_meat:
if (checked)
// Put some meat on the sandwich
else
// Remove the meat
break;
case R.id.checkbox_cheese:
if (checked)
// Cheese me
else
// I'm lactose intolerant
break;
// TODO: Veggie sandwich
}
}
I hope you find this helpful.我希望你觉得这有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.