Java 获取android中动态单选按钮的文本
我想知道如何在android上获取选中的动态创建单选按钮的文本。我被困住了,需要在stackoverflow社区寻求帮助 我还想,当我只按下按钮时,它会使文本烤熟 希望有人能给我指出正确的方向 谢谢 MainActivity.javaJava 获取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
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();
谢谢你的帮助,谢谢你的帮助,谢谢你的帮助,谢谢你的帮助,谢谢你的帮助,谢谢你的帮助