相关疑难解决方法(0)

Android:RadioGroup - 如何配置事件侦听器

根据我的理解,要确定是否"单击"复选框并查找是否已选中,可以使用以下代码:

cb=(CheckBox)findViewById(R.id.chkBox1);
        cb.setOnCheckedChangeListener(this);

public void onCheckedChanged(CompoundButton buttonView, 
    boolean isChecked) { 
        if (isChecked) { 
            cb.setText("This checkbox is: checked"); 
        } 
        else { 
            cb.setText("This checkbox is: unchecked"); 
        } 
    }
Run Code Online (Sandbox Code Playgroud)

但是,我无法弄清楚如何对无线电组进行上述操作的逻辑.

这是我的RadioGroup的xml:

<RadioGroup android:id="@+id/radioGroup1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content">
    <RadioButton android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/radio1" android:checked="true" 
    android:text="RadioButton1">
    </RadioButton>
    <RadioButton android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/radio2" android:text="RadioButton2" android:checked="true">
    </RadioButton>
    <RadioButton android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/radio3" android:text="RadioButton3">
    </RadioButton>
</RadioGroup>
Run Code Online (Sandbox Code Playgroud)

问题:我是否需要设置另一个监听器,或者监听器是否已经"注册"了这个组?

此外,应该在RadioGroup还是RadioButton上设置监听器?

android listener radio-group android-radiogroup android-radiobutton

36
推荐指数
4
解决办法
8万
查看次数