[Android] RadioGroup.check (R.id.xxx) and RadioButton.setChecked (xxx) different

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_38950819/article/details/85161321

These two days to do the project using RadioGroup and RadioButton, however, was faced with a wonderful question? Here I will demonstrate to the next by a small example of this problem is a wonderful horse look like God.

1, first of all, the layout file as follows:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".RadioGroupActivity">

    <RadioGroup
        android:id="@+id/rg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/low"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="低"/>
        <RadioButton
            android:id="@+id/medium"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="中"/>

        <RadioButton
            android:id="@+id/high"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="高"/>

    </RadioGroup>

    <Button
        android:id="@+id/btSetting"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="设置"/>

</LinearLayout>

2, RadioGroupActivity follows:

public class RadioGroupActivity extends AppCompatActivity {

    private RadioGroup radioGroup;
    private Button btSet;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_radio_group);

        radioGroup = findViewById(R.id.rg);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch(checkedId){
                    case R.id.low:
                        Log.d("RadioGroupActivity", "低");
                        break;

                    case R.id.medium:
                        Log.d("RadioGroupActivity", "中");
                        break;

                    case R.id.high:
                        Log.d("RadioGroupActivity", "高");
                        break;
                }
            }
        });

        btSet = findViewById(R.id.btSetting);
        btSet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                radioGroup.check(R.id.low);   //该方式会让onCheckedChanged()多次调用

                //该方法只执行一次
                RadioButton low = findViewById(R.id.low);
                low.setChecked(true);
            }
        });

    }
}

 3, let's do this: first select the "high", then "Settings" button is set to "low" to see print log of the situation.

4, the first implementation: In the event click the "Settings" button, we use radioGroup.check (R.id.low) method to achieve, log print results are as follows:

//选中“高”打印的log
2018-12-21 14:29:35.424 10893-10893/com.example.a0306.androiduipro D/RadioGroupActivity: 高 
//下面3条是点击“设置”按钮打印的log
2018-12-21 14:29:40.504 10893-10893/com.example.a0306.androiduipro D/RadioGroupActivity: 高
2018-12-21 14:29:40.522 10893-10893/com.example.a0306.androiduipro D/RadioGroupActivity: 低
2018-12-21 14:29:40.523 10893-10893/com.example.a0306.androiduipro D/RadioGroupActivity: 低

 5, second implementation: In the event click the "Settings" button, we use RadioButton low = findViewById (R.id.low); low.setChecked (true);

Implementation, log print results are as follows:

//选中“高”打印的log
2018-12-21 14:37:07.943 11484-11484/com.example.a0306.androiduipro D/RadioGroupActivity: 高
//点击“设置”按钮打印的log
2018-12-21 14:37:10.415 11484-11484/com.example.a0306.androiduipro D/RadioGroupActivity: 低

6, comparison of the above two implementations of the printing log can be found,

When using radioGroup.check (R.id.low) method, radioGroup listener callback onCheckedChanged () method is called multiple times;

The use low.setChecked (true) method, radioGroup listener callback onCheckedChanged () method is called only once.

7, using Android today, also today, only to find two methods RadioGroup.check () and RadioButton.setChecked () yet there are so different, victims of their own to do the project encountered problems locating a long time to find their roots, this the difference is certainly different ways to call an internal source.

Share a blogger for analysis traced the problem of https://drprincess.github.io/2018/05/27/Android-%E4%B8%BA%E4%BB%80%E4%B9%88% 20RadioGroup.onCheckedChanged ()% 20% E4% BC% 9A% E8% B0% 83% E7% 94% A8% E5% A4% 9A% E6% AC% A1% EF% BC% 9F /

Guess you like

Origin blog.csdn.net/qq_38950819/article/details/85161321