Получить текст динамического радио-кнопки в Android - PullRequest
0 голосов
/ 18 мая 2018

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

Ответы [ 2 ]

0 голосов
/ 18 мая 2018

Создайте 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, позвоните по этому номеру:

radioButton.getText();
0 голосов
/ 18 мая 2018

Измените RadioGroup r на private RadioGroup r.(Вы можете поставить его под Button btnPress1,btnPress2;)

In onClick() -> switch -> case

int checkedRadioButtonId = r.getCheckedRadioButtonId();
RadioButton radioButton = findViewById(checkedRadioButtonId);
String text = radioButton.getText();
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...