Java 在同一XML布局中区分两个无线电组

Java 在同一XML布局中区分两个无线电组,java,android,xml,Java,Android,Xml,我有两个无线电组在同一个xml布局中。我正在MainActivity.java文件中使用oncheckedChanged侦听器。我想用它的听众来区分这两个广播组。如何在侦听器中使用RadioGroup的参数 @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId) { case R.id.rexcellent: Toast.make

我有两个无线电组在同一个xml布局中。我正在MainActivity.java文件中使用oncheckedChanged侦听器。我想用它的听众来区分这两个广播组。如何在侦听器中使用RadioGroup的参数

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {

    switch (checkedId) {

    case R.id.rexcellent:

        Toast.makeText(getApplicationContext(), "execellnt",
                Toast.LENGTH_SHORT).show();

        break;
    case R.id.rverygood:
        Toast.makeText(getApplicationContext(), "very good",
                Toast.LENGTH_SHORT).show();

        break;
    case R.id.rgood:
        Toast.makeText(getApplicationContext(), "good", Toast.LENGTH_SHORT)
                .show();

        break;
    case R.id.raverage:
        Toast.makeText(getApplicationContext(), "average",
                Toast.LENGTH_SHORT).show();

        break;
    case R.id.rmale:
        Toast.makeText(getApplicationContext(), "Male",
                Toast.LENGTH_SHORT).show();

        break;
    case R.id.rfemale:
        Toast.makeText(getApplicationContext(), "female",
                Toast.LENGTH_SHORT).show();

        break;

    }

} 
以下是我的Xml代码:


我只想使用switch case语句。并且想知道在侦听器中使用RadioGroup的用法。它的作用是什么?

射线组组是发起单击事件的射线组。但是,在您的特殊情况下,我认为使用两个单独的事件处理程序会更为明显,因为两个不同的无线组之间绝对没有代码重用。

好吧,我的代码工作得非常好。。。。但是我不想知道我们是否想在不在MainActivity中创建单独的两个侦听器的情况下区分两个广播组。jave…可能吗?@user3763594是的,这是可能的,当两个广播组在某些方面相似时,这是完全可以接受的。在这种情况下,你的两个广播组没有共同点,因此最好有两个单独的听众。为什么不投票?请说明原因。有人在玩否决票。不知道是谁否决了它,只需使用组。getId@pskink我必须使用两个switch case语句吗?一个外部来区分组和一个内部?怎么做呢?在你的情况下,你不必使用两个开关,因为每个单选按钮都有其唯一的id
    <TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Rate Smartherd Tutorials ?"
    android:textAppearance="?android:attr/textAppearanceLarge" />

    <RadioGroup
    android:id="@+id/rgroup"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <RadioButton
        android:id="@+id/rexcellent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="Excellent" />

    <RadioButton
        android:id="@+id/rverygood"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Very Good" />

    <RadioButton
        android:id="@+id/rgood"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Good" />

    <RadioButton
        android:id="@+id/raverage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Average" />
</RadioGroup>

<TextView
    android1:id="@+id/textView2"
    android1:layout_width="wrap_content"
    android1:layout_height="wrap_content"
    android1:text="How is shreks ?" />

<RadioGroup
    android1:id="@+id/rg2"
    android1:layout_width="wrap_content"
    android1:layout_height="wrap_content" >

    <RadioButton
        android1:id="@+id/rmale"
        android1:layout_width="wrap_content"
        android1:layout_height="wrap_content"
        android1:checked="true"
        android1:text="MALE" />

    <RadioButton
        android1:id="@+id/rfemale"
        android1:layout_width="wrap_content"
        android1:layout_height="wrap_content"
        android1:text="Female" />

    </RadioGroup>

</LinearLayout>