开发者

Android, Checkbox listener in XML?

It's possible, to do things like this

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>

Java:

public void launchContactAdder(View v)
{
    Intent i = new Intent(this, ContactAdder.class);
    startActivity(i);
}

but there is a requirement, that the method must be public, void and the most important take View as an argument.

Now i'd like to do exactly the same thing but with Checkbox button. Checkbox has android:onclick attribute, but in Android tutorial (http://developer.android.com/resources/samples/ContactManager/index.html) I can see this code

showInvisibleControl.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
    {
        Log.d(TAG, "mShowInvisibleControl changed: " + isChecked);
        showInvisible = isChecked;
        populateContactList();
    }
});

So there is a onCheckedChanged(CompoundButton buttonView, boolean isChecked) method. Is there any way to do this by XML? There's no android:onCheckedChange attribute, only android:onClick attribute, but as I wrote above, name of that attribute must have corresponding method name, which takes View as an argument, but from the code above I understand that i must have a method with CompoundButton and boolean arguments.

Any way to do that in "XML way"?


Official documentation: 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"/>


It is now possible to do this with the use of data binding library. First you need to create a handler class (in this example, I call it MainActivityHandlers). Inside this handler class, define a method (for example the name is method1) with the corresponding implementation Then in your layout file, you just need to implement :

  ...
 <data>
    <variable
        name="handler"
        type="com.test.android.testapp.MainActivityHandlers" />
</data>
...
         <RadioGroup
        ...
        android:onCheckedChanged="@{handler.method1}"
        ...
        >

and you're good to go. More info : https://developer.android.com/topic/libraries/data-binding/index.html


I believe using a lambda is more convenient:

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/firstName"
    android:text="@={user.firstName}" />
<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onCheckedChanged="@{()->handler.checked(firstName)}" />

For more details see this.

Also, I find the use of two way binding very useful for checkboxes:

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checked='@={user.isChecked}'/>

Note that onCheckedChanged is called before the two way binding sets the value.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜