Android 材质按钮切换组 [英] Android Material Button Toggle Group

查看:35
本文介绍了Android 材质按钮切换组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 android 中创建一个要求性别的表单.为了获得这个输入,我使用了包含两个按钮的 Material Button Toggle Group.我不知道如何知道在我的 activity.java 中点击了哪个按钮.如何了解我的活动中选定的按钮,以便我可以将详细信息保存在不同的数据库中.

myxml.xml

 <按钮android:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"机器人:文本=男"android:layout_marginStart="5dp"style="?attr/materialButtonOutlinedStyle"/><按钮android:id="@+id/button2"android:layout_width="wrap_content"android:layout_height="wrap_content"机器人:文本=女性"android:layout_marginStart="10dp"style="?attr/materialButtonOutlinedStyle"/></com.google.android.material.button.MaterialButtonToggleGroup>

Myactivity.java

 MaterialButtonToggleGroup toggleButton = findViewById(R.id.toggleButton);toggleButton.addOnButtonCheckedListener();//我找不到任何合适的解决方案

解决方案

您可以使用 getCheckedButtonId() 方法.
类似的东西:

MaterialButtonToggleGroup materialButtonToggleGroup =findViewById(R.id.toggleButton);int buttonId = materialButtonToggleGroup.getCheckedButtonId();MaterialButton 按钮 = materialButtonToggleGroup.findViewById(buttonId);

<小时>

仅当您需要监听器时,您才可以使用 addOnButtonCheckedListener:

materialButtonToggleGroup.addOnButtonCheckedListener(new MaterialButtonToggleGroup.OnButtonCheckedListener() {@覆盖public void onButtonChecked(MaterialButtonToggleGroup 组,int checkedId,boolean isChecked) {如果(已检查){如果(checkedId == R.id.button1){//..}}}});

您必须检查 checkedId 值以及 isChecked 值.当您选中一个按钮以及取消选中一个按钮时,都会调用相同的侦听器.

这意味着如果您单击 button1,则使用 isChecked=truecheckedId=1 调用侦听器.然后如果你点击 button2 监听器会被调用两次.一次使用 isChecked=falsecheckedId=1,一次使用 isChecked=truecheckedId=2.

I'm creating a form in android which asks for gender. To get this input I use Material Button Toggle Group which contains two buttons. I don't know how to know which button is clicked in my activity.java. How to get to know about the selected button in my activity so that i can save the details in different database.

myxml.xml

    <com.google.android.material.button.MaterialButtonToggleGroup
                android:id="@+id/toggleButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:singleSelection="true">
                <Button
                    android:id="@+id/button1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Male"
                    android:layout_marginStart="5dp"
                    style="?attr/materialButtonOutlinedStyle"/>
                <Button
                    android:id="@+id/button2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Female"
                    android:layout_marginStart="10dp"
                    style="?attr/materialButtonOutlinedStyle"/>

            </com.google.android.material.button.MaterialButtonToggleGroup>

Myactivity.java

    MaterialButtonToggleGroup toggleButton = findViewById(R.id.toggleButton);
    toggleButton.addOnButtonCheckedListener(); 
    // I CAN'T FIND ANY PROPER SOLUTION

解决方案

You can use the getCheckedButtonId() method.
Something like:

MaterialButtonToggleGroup materialButtonToggleGroup = 
         findViewById(R.id.toggleButton);
int buttonId = materialButtonToggleGroup.getCheckedButtonId();
MaterialButton button = materialButtonToggleGroup.findViewById(buttonId);


Only if you need a listener you can use the addOnButtonCheckedListener:

materialButtonToggleGroup.addOnButtonCheckedListener(new MaterialButtonToggleGroup.OnButtonCheckedListener() {
      @Override
      public void onButtonChecked(MaterialButtonToggleGroup group, int checkedId, boolean isChecked) {
        if (isChecked) {
          if (checkedId == R.id.button1) {
            //..
          }
        }
  }
});

You have to check the checkedId value but also the isChecked value. The same listener is called when you check a button but also when you unckeck a button.

It means that if you click the button1 the listener is called with isChecked=true and checkedId=1. Then if you click the button2 the listener is called twice. Once with isChecked=false and checkedId=1, once with isChecked=true and checkedId=2.

这篇关于Android 材质按钮切换组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆