java - 从 radio 组获取和返回文本

标签 java android radio-button android-studio-3.0 android-radiogroup

我有一个广播组,可以选择"is"、“否”和“也许”。我想要一个函数来返回用户选择的选项。

这是我的代码

 <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);
            }
        });

最佳答案

您应该为 radioButton 使用 CustomListener

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;
            }
    }
}

关于java - 从 radio 组获取和返回文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56163972/

相关文章:

android - Android屏幕底部的编辑文本

android - 仅重绘 Android View 中的一部分

jquery 移动水平单选按钮等于按钮页面宽度百分比

java - JApplet 动画未运行

java - 什么时候适合使用 EAR,什么时候你的应用应该在 WAR 中?

java - 我有下面的方法来获取二维数组和一个值。该方法检查值是否在数组中

java - 打印 Getter 方法

android - touchlisteners 的 GridView 问题

mysql - 如何从 mysql 设置 cakephp 单选按钮?

php - 此表单使用单选按钮是否可以避免 SQL 注入(inject)?