Java 当单选按钮选中状态更改时,如何从单选组中的单选按钮获取文本

Java 当单选按钮选中状态更改时,如何从单选组中的单选按钮获取文本,java,android,radio-button,Java,Android,Radio Button,我在一个单选组中动态创建了两个单选按钮,其中一个被选中。 我需要当我选中另一个按钮时,它的值应该保存在字符串中。 但是我已经为此实现了checkedchangelistener,但这不是第一次。 这是我的密码 rg = ((RadioGroup)getActivity().findViewById(alist_id.get(i))); rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){ publi

我在一个单选组中动态创建了两个单选按钮,其中一个被选中。 我需要当我选中另一个按钮时,它的值应该保存在字符串中。 但是我已经为此实现了
checkedchangelistener
,但这不是第一次。 这是我的密码

rg = ((RadioGroup)getActivity().findViewById(alist_id.get(i)));
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
    public void onCheckedChanged(RadioGroup rd, int checkedId) {
        for(int i=0; i<rd.getChildCount(); i++) {
            radio_button = (RadioButton) rd.getChildAt(i);
            int id = radio_button.getId();
            if(radio_button.getId() == checkedId) {
                text = radio_button.getText().toString();
                flag=true;
                System.out.println("trueeeeeeeee"+text);
                return;
            }
        }
    }
});
if (flag==true) {
    updated_list.add(text);
    System.out.println("sssssssssssssssssss");
}else {
    updated_list.add(data_from_list_view.get(i));
    System.out.println("falseeeeeee");
}
rg=((RadioGroup)getActivity().findViewById(alist_id.get(i));
rg.setOnCheckedChangeListener(新的RadioGroup.OnCheckedChangeListener(){
检查更改后的公共无效(放射组rd,int checkedId){

对于(int i=0;i请尝试以下检索文本的解决方案:

radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

     void onCheckedChanged(RadioGroup rg, int checkedId) {
          for (int i = 0; i < rg.getChildCount(); i++) {
               RadioButton btn = (RadioButton) rg.getChildAt(i);
               if (btn.getId() == checkedId) {
                   String text = btn.getText();
                   // do something with text
                   return;
               }
          }
     }
});
radiogroup.setOnCheckedChangeListener(新的radiogroup.OnCheckedChangeListener(){
检查更改后无效(放射组rg,int checkedId){
对于(int i=0;i
尝试以下代码

radiogroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            int id=group.getCheckedRadioButtonId();
            RadioButton rb=(RadioButton) findViewById(id);
                                     OR
            RadioButton rb=(RadioButton) findViewById(checkedId);

            String radioText=rb.getText().toString();

        }
    });
试试这个

    radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            RadioButton radioButton = (RadioButton) group.findViewById(checkedId);
            String text = radioButton.getText().toString();
            updated_list.add(text);
        }
    });
xml文件

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

<RadioGroup
    android:id="@+id/radioSex"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <RadioButton
        android:id="@+id/radioMale"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio_male" 
        android:checked="true" />

    <RadioButton
        android:id="@+id/radioFemale"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio_female" />

</RadioGroup>

<Button
    android:id="@+id/btnDisplay"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/btn_display" />

</LinearLayout>

从片段中的放射组获取文本的Kotlin版本如下所示:

private lateinit var genderRadioGroup: RadioGroup

genderRadioGroup = rootView.gender_radio_grp

val selectedRadioButton: Int = genderRadioGroup.checkedRadioButtonId
        radioButton = rootView.findViewById(selectedRadioButton)
        Toast.makeText(context, "Selected gender is ${radioButton.text}", Toast.LENGTH_SHORT).show()
(顺便说一句,我正在使用Android扩展来获取视图的引用)

radioGroup.setOnCheckedChangeListener{group,checkedId->
group.findViewById(checkedId)?.let{radioButton->
val textFromButtonLabel=radioButton.text.toString()
}
}

Kotlin从RadioGroup中选中的单选按钮检索文本的代码

    binding.genderRadiogroup.setOnCheckedChangeListener { _, _ ->
        var id: Int = binding.genderRadiogroup.checkedRadioButtonId
        var rb: RadioButton = binding.genderRadiogroup.findViewById(id)
        Toast.makeText(this,rb.text.toString(),Toast.LENGTH_SHORT).show()
    }

由于选择了单选按钮,是否会出现问题?@amita已设置
radiogroup。检查(IdOfYourButton)
,因为在这种情况下,默认情况下您的单选按钮处于选中状态,因此在这种情况下,侦听器不会第一次被调用。AndroidWarrior不,我没有。在创建时,我必须设置?我正在使用此代码动态创建单选按钮final RadioButton[]rb=new RadioButton[list_of_RadioButton_data.size()];rg=new RadioGroup(getActivity());rg.setId(i);rg.setOrientation(RadioGroup.VERTICAL);for(int i1=0;i1android:checkedButton=“@+Id/radioMale”来检查
RadioButton
Id
RadioGroup
中。如果您直接单击按钮,则可能会出现错误
NullPointerException
radioGroup.setOnCheckedChangeListener { group, checkedId ->
            group.findViewById<AppCompatRadioButton>(checkedId)?.let { radioButton ->
                val textFromButtonLabel = radioButton.text.toString()
            }
        }
    binding.genderRadiogroup.setOnCheckedChangeListener { _, _ ->
        var id: Int = binding.genderRadiogroup.checkedRadioButtonId
        var rb: RadioButton = binding.genderRadiogroup.findViewById(id)
        Toast.makeText(this,rb.text.toString(),Toast.LENGTH_SHORT).show()
    }