kam*_*ych 11 xml checkbox android listener
做这样的事情是可能的
XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/addContactButton"
android:text="@string/addContactButtonLabel"
android:onClick="launchContactAdder"/><!-- here -->
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
Java的:
public void launchContactAdder(View v)
{
Intent i = new Intent(this, ContactAdder.class);
startActivity(i);
}
Run Code Online (Sandbox Code Playgroud)
但有一个要求是,该方法必须是公开的,无效的,最重要的是将View作为参数.
现在我想用Checkbox按钮做同样的事情.Checkbox有android:onclick属性,但是在Android教程(http://developer.android.com/resources/samples/ContactManager/index.html)中我可以看到这段代码
showInvisibleControl.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
Log.d(TAG, "mShowInvisibleControl changed: " + isChecked);
showInvisible = isChecked;
populateContactList();
}
});
Run Code Online (Sandbox Code Playgroud)
所以有一个onCheckedChanged(CompoundButton buttonView,boolean isChecked)方法.有没有办法通过XML来做到这一点?没有android:onCheckedChange属性,只有android:onClick属性,但正如我上面写的那样,该属性的名称必须有相应的方法名称,它将View作为参数,但从上面的代码我明白我必须有一个方法CompoundButton和boolean参数.
有什么办法以"XML方式"做到这一点?
小智 6
官方文档:https://developer.android.com/guide/topics/ui/controls/checkbox.html
<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"/>
Run Code Online (Sandbox Code Playgroud)