【问题标题】:How to get the text from radio button in a radio group when radio button checked state is changed更改单选按钮选中状态时如何从单选按钮组中的单选按钮获取文本
【发布时间】:2013-12-06 22:54:37
【问题描述】:

我在一个单选组中动态创建了两个单选按钮,其中一个被选中。 当我检查另一个按钮时,我需要它的值应该保存在字符串中。 但我已经为此实现了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");
}

【问题讨论】:

    标签: java android radio-button


    【解决方案1】:

    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>
    

    java代码

    package com.mkyong.android;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.RadioButton;
    import android.widget.RadioGroup;
    import android.widget.Toast;
    
    public class MyAndroidAppActivity extends Activity {
        private RadioGroup radioSexGroup;
        private RadioButton radioSexButton;
        private Button btnDisplay;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        addListenerOnButton();
    
        }
    
        public void addListenerOnButton() {
    
            radioSexGroup = (RadioGroup) findViewById(R.id.radioSex);
            btnDisplay = (Button) findViewById(R.id.btnDisplay);
    
            btnDisplay.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
    
                        // get selected radio button from radioGroup
                    int selectedId = radioSexGroup.getCheckedRadioButtonId();
    
                    // find the radiobutton by returned id
                        radioSexButton = (RadioButton) findViewById(selectedId);
    
                    Toast.makeText(MyAndroidAppActivity.this,
                        radioSexButton.getText(), Toast.LENGTH_SHORT).show();
    
                }
    
            });
    
        }
    }
    

    【讨论】:

    • 会不会是因为选中的单选按钮有问题?
    • 对于这个答案,您必须使用RadioGroup 中的属性android:checkedButton="@+id/radioMale" 检查RadioButton Id。如果您直接单击按钮,则可能会出现错误NullPointerException
    【解决方案2】:

    试试下面的代码

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

    【讨论】:

    • 我创建了如下动态单选按钮:-
    • 最终 RadioButton[] rb = new RadioButton[list_of_radiobutton_data.size()]; rg = new RadioGroup(getActivity()); rg.setId(i); rg.setOrientation(RadioGroup.VERTICAL); for (int i1 = 0; i1
    【解决方案3】:

    试试这个...

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

    【讨论】:

    • 这段代码正确吗?我必须实施哪些更改才能调用第一次检查更改侦听器?
    • Radiogroup.checkedChangeListener 不是第一次调用。
    • 在 cmets 部分编写的代码不可读。只需编辑您的答案并写在那里...
    • 最终 RadioButton[] rb = new RadioButton[list_of_radiobutton_data.size()]; rg = new RadioGroup(getActivity()); rg.setId(i); rg.setOrientation(RadioGroup.VERTICAL); for (int i1 = 0; i1
    【解决方案4】:

    用于从 RadioGroup 中选中的 RadioButton 中检索文本的 Kotlin 代码

        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()
        }
    

    【讨论】:

      【解决方案5】:

      试试这个解决方案来检索文本:

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

      【讨论】:

      • 会不会是因为选中的单选按钮有问题?
      • @amita 你设置了radiogroup.check(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; i1
      • 这段代码正确吗?我必须实施哪些更改才能调用第一次检查更改侦听器?
      【解决方案6】:

      从片段中的 RadioGroup 获取文本的 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()
      

      (P.S. 我正在使用 Android Extension 来获取视图的引用)

      【讨论】:

        【解决方案7】:
        radioGroup.setOnCheckedChangeListener { group, checkedId ->
                    group.findViewById<AppCompatRadioButton>(checkedId)?.let { radioButton ->
                        val textFromButtonLabel = radioButton.text.toString()
                    }
                }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-07-21
          • 1970-01-01
          • 1970-01-01
          • 2011-07-28
          • 1970-01-01
          • 2021-12-29
          • 1970-01-01
          • 2017-07-24
          相关资源
          最近更新 更多