【问题标题】:Get the text of dynamic radio button in android获取android中动态单选按钮的文本
【发布时间】:2018-10-28 21:00:16
【问题描述】:

我试图弄清楚如何在 android 上获取所选动态创建的单选按钮的文本。我被卡住了,需要在 stackoverflow 社区寻求帮助。

我也想当我只按下按钮时它会烤文本。

希望有人能指出正确的方向。

谢谢

MainActivity.java

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    Button btnPress1,btnPress2;

    String names[] = {"Braces Adjustment","Tooth Extraction","Cleaning"};
    RadioButton r1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnPress1 = (Button) findViewById(R.id.btnPress1);
        btnPress2 = (Button) findViewById(R.id.btnPress2);

        LinearLayout l = (LinearLayout) findViewById(R.id.linearLayout);
        RadioGroup r = new RadioGroup(this);
        r.setOrientation(RadioGroup.VERTICAL);
        RadioGroup.LayoutParams rl;

        //This is where i generate a dynamic radio button
        for(int i=0; i<names.length;i++){
            r1= new RadioButton(this);
            r1.setId(i + 1);
            r1.setText(names[i]);
            r1.setButtonDrawable(R.drawable.custom_radio_button);
            r1.setPadding(10,10,10,10);

            r1.setGravity(Gravity.CENTER_VERTICAL);
            rl=new RadioGroup.LayoutParams(RadioGroup.LayoutParams.MATCH_PARENT,RadioGroup.LayoutParams.MATCH_PARENT);
            rl.setMargins(10,15,0,0);
            r.addView(r1,rl);
        }
        l.addView(r);

    }



@Override
    public void onClick(View view) {

        switch (view.getId()){
            case R.id.btnPress1:
                //Toast here the text of radio button i selected but when i press the button
                Toast.makeText(MainActivity.this,"BUTTON1",Toast.LENGTH_SHORT).show();
                break;
            case R.id.btnPress2:
                //Toast here the text of radio button i selected but when i press the button
                Toast.makeText(MainActivity.this,"BUTTON2",Toast.LENGTH_SHORT).show();
                break;
        }
    }

【问题讨论】:

    标签: java android radio-button


    【解决方案1】:

    RadioGroup r 更改为private RadioGroup r。 (可以放在Button btnPress1,btnPress2;下)

    onClick() -&gt; switch -&gt; case

    int checkedRadioButtonId = r.getCheckedRadioButtonId();
    RadioButton radioButton = findViewById(checkedRadioButtonId);
    String text = radioButton.getText();
    

    【讨论】:

    • 感谢您的帮助,不胜感激
    【解决方案2】:

    将您的RadioGroup 设为字段:

    private RadioGroup radioGroup;
    

    onCreate 中分配您创建的 RadioGroup:

    radioGroup = new RadioGroup(this);
    

    获取选中的单选按钮:

    // get the checked radiobutton id from the radio group
    int radioButtonID = radioButtonGroup.getCheckedRadioButtonId();
    
    // get the radiobutton
    RadioButton radioButton = radioButtonGroup.findViewById(radioButtonID);
    

    要获取RadioButton 的文本,请调用:

    radioButton.getText();
    

    【讨论】:

    • 感谢您的帮助,不胜感激
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-06
    • 1970-01-01
    • 1970-01-01
    • 2020-06-11
    • 2021-12-29
    相关资源
    最近更新 更多