Я пытаюсь выяснить, как получить текст выбранной радиокнопки на андроиде.Я застрял, что нужно обратиться за помощью к сообществу 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;
}
}