Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 获取android中动态单选按钮的文本_Java_Android_Radio Button - Fatal编程技术网

Java 获取android中动态单选按钮的文本

Java 获取android中动态单选按钮的文本,java,android,radio-button,Java,Android,Radio Button,我想知道如何在android上获取选中的动态创建单选按钮的文本。我被困住了,需要在stackoverflow社区寻求帮助 我还想,当我只按下按钮时,它会使文本烤熟 希望有人能给我指出正确的方向 谢谢 MainActivity.java public class MainActivity extends AppCompatActivity implements View.OnClickListener{ Button btnPress1,btnPress2; String na

我想知道如何在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;
        }
    }
public类MainActivity扩展AppCompatActivity实现View.OnClickListener{
按钮btnPress1、btnPress2;
字符串名称[]={“牙套调整”、“拔牙”、“清洁”};
单选按钮r1;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnPress1=(按钮)findViewById(R.id.btnPress1);
btnPress2=(按钮)findViewById(R.id.btnPress2);
LinearLayout l=(LinearLayout)findViewById(R.id.LinearLayout);
射线组r=新射线组(此);
r、 设置方向(放射组垂直);
RadioGroup.LayoutParams rl;
//这是我生成动态单选按钮的地方

对于(int i=0;i将
RadioGroup r
更改为
private RadioGroup r
(您可以将其放在
按钮btnPress1、btnPress2;下)

onClick()->switch->case中

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

RadioGroup r
更改为
private RadioGroup r
(您可以将其放在
按钮btnPress1、btnPress2;
下)

onClick()->switch->case中

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

将您的
RadioGroup
作为字段:

private RadioGroup radioGroup;
onCreate
中,分配您创建的放射组:

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

将您的
RadioGroup
作为字段:

private RadioGroup radioGroup;
onCreate
中,分配您创建的放射组:

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

谢谢你的帮助,谢谢你的帮助,谢谢你的帮助,谢谢你的帮助,谢谢你的帮助,谢谢你的帮助