机器人:如何获取由用户选择的动态创建的单选按钮的文本? [英] Android: How to get text of dynamically created radio button selected by the user?

查看:160
本文介绍了机器人:如何获取由用户选择的动态创建的单选按钮的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can i retrieve the text of a dynamically created radio button selected by the user? Here's my code:

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

等待回复,
Maqsood

Waiting for the reply, Maqsood

推荐答案

惊讶没有一个更简单的方法。如果你打算在此基础上按钮做一些特别的东西,虽然你也许应该检查ID而不是标签。

Surprised there isn't an easier way. If you are going to do something special though based on which button you should probably checked the ID instead of the Label.

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

这篇关于机器人:如何获取由用户选择的动态创建的单选按钮的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆