java - 编码多个 radio 组

标签 java android xml eclipse

我正在 Android Studio 中创建一个基本应用程序:

共有 3 个类别,每个类别都有一个单选组。

用户从每个类别中选择一个按钮,然后点击“选择”。

Textview 应该显示一条消息,例如“你选择了中文,一级,猜猜城市”。

我已经弄清楚如何说“你选择了中文”,但不知道如何使用 if 条件添加其余内容。有什么想法吗?

<TextView android:id="@+id/text" />
<RadioGroup android:id="@+id/radio1">
    <RadioButton id="@+id/chinese"
        android:text="@string/chinese" />
    <RadioButton android:id="@+id/indian"
        android:text="@string/indian" />
    <RadioButton android:id="@+id/russian"
        android:text="@string/russian" />
</RadioGroup>
<RadioGroup android:id="@+id/radio2">
    <RadioButton android:id="@+id/difficulty1"
        android:text="@string/starter" />
    <RadioButton android:id="@+id/difficulty2"
            android:text="@string/medium" />
    <RadioButton android:id="@+id/difficulty3"
            android:text="@string/expert" />
</RadioGroup>
<RadioGroup android:id="@+id/radio3">
    <RadioButton android:id="@+id/challenge1"
            android:text="@string/buildingbricks" />
    <RadioButton android:id="@+id/challenge2"
            android:text="@string/callcentre" />
    <RadioButton android:id="@+id/challenge3"
            android:text="@string/city" />
</RadioGroup>
<小时/>
public class MainActivity extends Activity {
    private RadioGroup radioGroup1
    private RadioButton chinese, indian, russian;
    private Button button;
    private TextView textView;

    public String string_first_radiogroup, 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        radioGroup1 = (RadioGroup) findViewById(R.id.radio1);


        radioGroup1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(checkedId == R.id.chinese) {
                    string_first_radiogroup="Chinese";
                } else if(checkedId == R.id.indian) {
                    string_first_radiogroup="Indian"; 
                } else {
                    string_first_radiogroup="Russian";
                }
            }
        });

        chinese = (RadioButton) findViewById(R.id.chinese);
        indian = (RadioButton) findViewById(R.id.indian);
        russian = (RadioButton) findViewById(R.id.russian);
        textView = (TextView) findViewById(R.id.text);


        button = (Button)findViewById(R.id.choose);
        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                textView.setText("You chose " + string_first_radiogroup + " option");
            }
        });
    }
}

最佳答案

尝试下面的代码:

button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                textView.setText("You chose " + ((RadioButton)findViewById(radioGroup1.getCheckedRadioButtonId())).getText() + " option");
            }
        });

关于java - 编码多个 radio 组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29007253/

相关文章:

java - 上传文件并将其存储到文件夹中?

java - 如何使用 MiGLayout 将组件居中放置在包含多个组件的行上

java - 如何在Java中动态映射同一父类的子对象列表

android - 使用引用恢复 fragment 实例

android - 如何读取app文件夹下的配置文件

Java 如何为循环中的每次迭代播放声音?

java - 如何使 ListView 可点击

android - 如何补偿 RelativeLayout 中的状态栏?

xml - 如何访问 upcdatabase.org API?

php - HTTP API 的排序结果