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

9

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

10

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

            }

        });

    }
}

1
选择单选按钮会有问题吗? - amita
对于这个问题,您必须使用属性android:checkedButton="@+id/radioMale"RadioGroup中检查RadioButton的ID。如果直接点击按钮,则可能会出现错误NullPointerException - Pratik Butani

6

尝试以下代码

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

        }
    });

我已经按照以下方式创建了动态单选按钮: - amita
最终的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 < list_of_radiobutton_data.size(); i1++) { rb[i1] = new RadioButton(getActivity()); rb[i1].setText(list_of_radiobutton_data.get(i1)); rb[i1].setId(i1); rg.addView(rb[i1]); if (rb[i1].getText().equals(data_from_list_view.get(i))) { /rb[i1].setChecked(true);/ rg.check(rb[i1].getId()); } } - amita

3

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

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

这段代码正确吗?第一次调用checked change监听器需要实现哪些更改? - amita
Radiogroup.checkedChangeListener第一次不会被调用。 - amita
在程序开发相关的内容中,翻译如下:不要在评论部分写代码,因为它们无法被读取。请编辑您的答案并将代码写在那里。 - Gopal Gopi
最终的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 < list_of_radiobutton_data.size(); i1++) { rb[i1] = new RadioButton(getActivity()); rb[i1].setText(list_of_radiobutton_data.get(i1)); rb[i1].setId(i1); rg.addView(rb[i1]); if (rb[i1].getText().equals(data_from_list_view.get(i))) { rb[i1].setChecked(true); /rg.check(rb[i1].getId());/ } } - amita

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

0

尝试使用以下解决方案来检索文本:

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
@amita 你有设置radiogroup.check(IdOfYourButton)吗?因为在这种情况下,你的单选按钮会默认选择,因此监听器不会在第一次调用时被调用。 - CodeWarrior
AndroidWarrior 没有,我需要在创建时进行设置吗? - amita
我正在使用以下代码动态创建单选按钮: 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 < list_of_radiobutton_data.size(); i1++) { rb[i1] = new RadioButton(getActivity()); rb[i1].setText(list_of_radiobutton_data.get(i1)); rb[i1].setId(i1); rg.addView(rb[i1]); if (rb[i1].getText().equals(data_from_list_view.get(i))) { rb[i1].setChecked(true); } } - amita
这段代码正确吗?我需要进行哪些更改才能调用第一次的选中状态监听器? - amita

0

Kotlin版本的从片段中RadioGroup获取文本的方法如下:

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扩展来获取视图的引用)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接