安卓:RadioGroup中 - 如何配置事件监听器监听器、事件、RadioGroup

由网友(戴着虚伪的面具继续做人)分享简介:这是我的理解,以确定是否一个复选框点击,发现如果它检查与否,code,例如可以使用以下内容:From my understanding, to determine if a checkbox is "clicked" and find if it's checked or not, code such as the...

这是我的理解,以确定是否一个复选框点击,发现如果它检查与否,code,例如可以使用以下内容:

From my understanding, to determine if a checkbox is "clicked" and find if it's checked or not, code such as the following can be used:

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"); 
        } 
    }

不过,我无法找出如何做上述的RadioGroup中的逻辑。

However, I am unable to work out the logic on how to do the above for a radiogroup.

下面是XML我RadioGroup中:

Here is the xml for my RadioGroup:

<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>

问:我是否需要安装其它监听,或将听者已经在那里也注册这个群体?

Question: Do i need to setup another listener, or will the listener already there also "register" this group?

此外,应听众建立在RadioGroup中或单选按钮?

Also, should the listener be set up on the RadioGroup or the RadioButton?

推荐答案

这是你如何让检查单选按钮:

This is how you get the checked radiobutton:

// This will get the radiogroup
RadioGroup rGroup = (RadioGroup)findViewById(r.id.radioGroup1);
// This will get the radiobutton in the radiogroup that is checked
RadioButton checkedRadioButton = (RadioButton)rGroup.findViewById(rGroup.getCheckedRadioButtonId());

要使用监听器,你这样做:

To use the listener, you do this:

// This overrides the radiogroup onCheckListener
rGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
    public void onCheckedChanged(RadioGroup rGroup, int checkedId)
    {
        // This will get the radiobutton that has changed in its check state
        RadioButton checkedRadioButton = (RadioButton)rGroup.findViewById(checkedId);
        // This puts the value (true/false) into the variable
        boolean isChecked = checkedRadioButton.isChecked();
        // If the radiobutton that has changed in check state is now checked...
        if (isChecked)
        {
            // Changes the textview's text to "Checked: example radiobutton text"
            tv.setText("Checked:" + checkedRadioButton.getText());
        }
    }
});
阅读全文

相关推荐

最新文章