Android:如何获取用户选择的动态创建单选按钮的文本?

Android:如何获取用户选择的动态创建单选按钮的文本?,android,Android,如何检索动态创建的单选按钮的文本 由用户选择?这是我的密码: RadioGroup radiogroup = (RadioGroup) findViewById(R.id.rdbGp1); // layout params to use when adding each radio button LinearLayout.LayoutParams layoutParams = new RadioGroup.LayoutParams(

如何检索动态创建的单选按钮的文本 由用户选择?这是我的密码:

RadioGroup radiogroup = (RadioGroup) findViewById(R.id.rdbGp1); 
        // layout params to use when adding each radio button 
        LinearLayout.LayoutParams layoutParams = new 
RadioGroup.LayoutParams( 
                RadioGroup.LayoutParams.WRAP_CONTENT, 
                RadioGroup.LayoutParams.WRAP_CONTENT); 
 for (int i = 0; i < 4; i++){ 
            final RadioButton newRadioButton = new RadioButton(this); 
            c3 = db.getAns(3); 
        for (int j=0;j<i;j++) 
            c3.moveToNext(); 
           label = c3.getString(0); 
        newRadioButton.setText(label); 
        newRadioButton.setId(6); 
        radiogroup.addView(newRadioButton, layoutParams); 
RadioGroup-RadioGroup=(RadioGroup)findViewById(R.id.rdbGp1);
//添加每个单选按钮时要使用的布局参数
LinearLayout.LayoutParams LayoutParams=新建
RadioGroup.LayoutParams(
RadioGroup.LayoutParams.WRAP_内容,
RadioGroup.LayoutParams.WRAP_内容);
对于(int i=0;i<4;i++){
最终RadioButton newRadioButton=新RadioButton(本);
c3=db.getAns(3);

对于(int j=0;j来说,令人惊讶的是,没有一个更简单的方法。如果你想做一些特别的事情,尽管你应该根据哪个按钮来检查ID,而不是标签

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

对于(inti=0;i我认为有一种更简单的方法来实现这一点

我刚刚创建了一个单选按钮,该按钮的id已选中,工作正常

解决方案如下所示

RadioButton TheTextIsHere = (RadioButton) findViewById(RadioGroup.getCheckedRadioButtonId());
现在你有了一个RadioButton,它指的是被签入RadioButton组的RadioButton,然后你可以很容易地

TheTextIsHere.getText().toString();

希望我能帮上忙:)

这是一个老问题,但这个答案可能会帮助其他人

我解决了从下面这样的
单选按钮
获取文本而不使用任何for循环的问题。 这对我来说是可行的,但我使用了xml,但我认为这一原则无论如何都是可行的

只有在预先检查无
RadioButton
的情况下,
/
后面的代码才是必需的,因为如果选择了无
RadioButton
,RadioBtnched将为-1。因此,应用程序将“崩溃”,因为
findviewbyid(-1)
无效。 至少在xml中,您可以使用
android:checked=“true”
预先检查
单选按钮

RadioGroup-RadioGroup-1=(RadioGroup)findViewById(R.id.RadioGroup-1);
int radioBtnChecked=radioGroup1.getCheckedRadioButtonId();

//如果(radioBtnCheckedHasMap在这种情况下更好。HashMap的设计是为了快速检索值……当然,您的具体案例只使用了“4个”单选按钮,因此您不会真正注意到差异。不过,我还是更喜欢这种解决方案

为HasMap创建成员变量:

Map<Integer, RadioButton> mapping = new HashMap<Integer, RadioButton>();

有一种很难做到的方法。 对于每个单选按钮,您需要有一个正整数作为ID。然后使用该ID,您可以引用所选单选按钮。代码如下:

private void addButtons(String[] taskNames) {

    //You can define your radio group this way
    //or you can define it in onCreate. NOTE: if you 
    //define it in onCreate, make sure you do a 
    //rGroup.removeAllViews() or rGroup.removeView(child)
    rGroup = new RadioGroup(this);

    //hash code is the ID we will give to the radio buttons
    int hash;

    //going through the list of names and making radio buttons 
    //out of them and putting them into a radio group
    for(String name : taskNames)
    {
        //making a button
        RadioButton button = new RadioButton(this);

        //setting the button's text
        button.setText(name);

        //setting the button's ID by finding it's hashCode
        //Note that the ID MUST be a positive number
        hash = Math.abs((name).hashCode());
        button.setId(hash);

        //adding to the radio button group
        rGroup.addView(button);     
    }

    //Then you can add the radio group to your desired layout from the xml file
    LinearLayout desiredLayout = (LinearLayout) findViewById(R.id.desireLinearLayout);
    desiredLayout.addView(rGroup);
}

//here is a how to get the checked radio button
private void onClickSubmit()
{
    //for instance you can add the name to a DB
    DatabaseHandler db = new DatabaseHandler(this);
    try 
    {
        //get the ID of the button (i.e. the hashCode we assigned to it
        int id = rGroup.getCheckedRadioButtonId();

        //Getting the radio button
        RadioButton rbChecked = (RadioButton) rGroup.findViewById(id);

        //getting the name of the radio button
        String rbName = rbChecked.getText().toString();

        //adding the name to the DB 
        db.addName(rbName);

        //showing a friendly message to the user that the operation has been successful 
        Toast.makeText(this, "Yay, name added", Toast.LENGTH_SHORT).show();
    } 
    catch (Exception e) 
    {
        Toast.makeText(this, "Can't submit", Toast.LENGTH_SHORT).show();
    }
}
HashCode是确定性的,所以使用它们是安全的,但是因为我们在做Math.abs,所以我们为两个东西哈希到相同值的可能性留出了一些空间,因为我们正在消除负部分。但是到目前为止,它对我来说工作得很好。
但是你可以做各种创造性的事情来避免冲突。我相信你会找到答案:)

谢谢,但是没有像btn.getId()的equals(checkedd)这样的方法。我在我的Eclipse编辑器中尝试过,但找不到它。请检查。然后只做getId()==checkedId。id只是一个int。我知道这是一个老问题,但HashMap不是一个更简单的解决方案吗(如我在回答中所解释的)?我很感激您的想法。Thnxbtn.getText()必须转换为字符串
{  
    ... // your for-loop
    int id = <your id here>
    newRadioButton.setId(id);        // set the id
    mapping.put(id, newRadioButton); // store the id as the key-value
    ... // continue with your for-loop
}
radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

    @Override
    public void onCheckedChanged(RadioGroup rg, int checkedId) 
    {
        String txt = ((RadioButton)mapping.get(checkedId)).getText();
        // do something with your text
    }
});
private void addButtons(String[] taskNames) {

    //You can define your radio group this way
    //or you can define it in onCreate. NOTE: if you 
    //define it in onCreate, make sure you do a 
    //rGroup.removeAllViews() or rGroup.removeView(child)
    rGroup = new RadioGroup(this);

    //hash code is the ID we will give to the radio buttons
    int hash;

    //going through the list of names and making radio buttons 
    //out of them and putting them into a radio group
    for(String name : taskNames)
    {
        //making a button
        RadioButton button = new RadioButton(this);

        //setting the button's text
        button.setText(name);

        //setting the button's ID by finding it's hashCode
        //Note that the ID MUST be a positive number
        hash = Math.abs((name).hashCode());
        button.setId(hash);

        //adding to the radio button group
        rGroup.addView(button);     
    }

    //Then you can add the radio group to your desired layout from the xml file
    LinearLayout desiredLayout = (LinearLayout) findViewById(R.id.desireLinearLayout);
    desiredLayout.addView(rGroup);
}

//here is a how to get the checked radio button
private void onClickSubmit()
{
    //for instance you can add the name to a DB
    DatabaseHandler db = new DatabaseHandler(this);
    try 
    {
        //get the ID of the button (i.e. the hashCode we assigned to it
        int id = rGroup.getCheckedRadioButtonId();

        //Getting the radio button
        RadioButton rbChecked = (RadioButton) rGroup.findViewById(id);

        //getting the name of the radio button
        String rbName = rbChecked.getText().toString();

        //adding the name to the DB 
        db.addName(rbName);

        //showing a friendly message to the user that the operation has been successful 
        Toast.makeText(this, "Yay, name added", Toast.LENGTH_SHORT).show();
    } 
    catch (Exception e) 
    {
        Toast.makeText(this, "Can't submit", Toast.LENGTH_SHORT).show();
    }
}