Получение и возврат текста из радиогруппы - PullRequest
0 голосов
/ 16 мая 2019

У меня есть радиогруппа с вариантами «Да», «Нет» и «Может быть».Мне нужна функция, которая будет возвращать тот выбор, который выбрал пользователь.

Вот мой код

 <RadioGroup
        android:id="@+id/answerRadioGroup"
        android:layout_width="240dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:orientation="horizontal"
        app:layout_constraintBottom_toTopOf="@+id/submitButton"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/myTextView"
        app:layout_constraintVertical_bias="0.26">

        <RadioButton
            android:id="@+id/yesRadioButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Yes" />

        <RadioButton
            android:id="@+id/noRadioButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="No" />

        <RadioButton
            android:id="@+id/maybeRadioButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Maybe" />

    </RadioGroup>

А вот функция, которую я пытаюсь использовать:

private String checkGuess(){
        guessRadioGroup = (RadioGroup) findViewById(R.id.guessRadioGroup);
        int selectedId = guessRadioGroup.getCheckedRadioButtonId();
        radioButton = (RadioButton) findViewById(selectedId);
        String s = (String) radioButton.getText().toString();
        return s;
    }

Эта функция работает, когда я заменяю String s = (String) radioButton.getText().toString(); на что-то вроде String s = "Whatever".Кажется, проблема в использовании метода getText().

Это то, что я использую в MainActivity.java для отображения текста:

TextView tempTextView = (TextView) findViewById(R.id.tempTextView);
        tempTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String abc = checkGuess();
                tempTextView.setText("You chose: " + abc);
            }
        });

Ответы [ 2 ]

0 голосов
/ 16 мая 2019

Вы должны использовать свою радиогруппу в качестве родительского для радио-кнопки поиска. А также вам не нужно приводить как String при использовании toString ().

private String checkGuess(){
    guessRadioGroup = (RadioGroup) findViewById(R.id.guessRadioGroup);
    int selectedId = guessRadioGroup.getCheckedRadioButtonId();
    radioButton = (RadioButton) guessRadioGroup.findViewById(selectedId);
    String s = radioButton.getText().toString();
    return s;
}

Надеюсь, это поможет вам.

0 голосов
/ 16 мая 2019

Вы должны использовать CustomListener для radioButton

radioButton.setOnCheckedChangeListener(new CustomListener());
class CustomListener implements   CompoundButton.OnCheckedChangeListener{

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            switch (buttonView.getId()){
                case R.id.rd1:

                    break;
            }
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...