Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 单选按钮组的问题_Android_Radio Button - Fatal编程技术网

Android 单选按钮组的问题

Android 单选按钮组的问题,android,radio-button,Android,Radio Button,我正在尝试使用单选按钮组小部件在我的应用程序中选择性别。事情是这样的。用户将选择一个性别,点击按钮,用户将被引导到相应的活动。我写的代码。第二个问题是,尽管我在if部分启动了新的活动,但我的程序什么也没做。我调试了我的程序,看看if条件是否满足,在我看来,它确实满足了。下面是xml和.java文件 public class Gender extends Activity{ private RadioGroup radioSexGroup; private RadioButt

我正在尝试使用单选按钮组小部件在我的应用程序中选择性别。事情是这样的。用户将选择一个性别,点击按钮,用户将被引导到相应的活动。我写的代码。第二个问题是,尽管我在if部分启动了新的活动,但我的程序什么也没做。我调试了我的程序,看看if条件是否满足,在我看来,它确实满足了。下面是xml和.java文件

public class Gender extends Activity{
     private RadioGroup radioSexGroup;
      private RadioButton radioSexButton;
      private Button btnDisplay;

      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gender_asking);

        addListenerOnButton();

      }

      public void addListenerOnButton() {

        radioSexGroup = (RadioGroup) findViewById(R.id.radioSex);
        btnDisplay = (Button) findViewById(R.id.gender_button);

        btnDisplay.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
     //****************************
                    int selectedId = radioSexGroup.getCheckedRadioButtonId();

                    // find the radiobutton by returned id
                        radioSexButton = (RadioButton) findViewById(selectedId);
                    if("Iam a Guy!".equals(radioSexButton.getText()))
                    {
                        Intent male_activity = new Intent(Gender.this, Meter.class);
                        startActivity(male_activity);

                    }
                    if("Iam a Girl!".equals(radioSexButton.getText()))
                    {
                        Intent male_activity = new Intent(Gender.this, For_Girl.class);
                        startActivity(male_activity);

                    }

                //********************************     

            }

        });

      }
}
关联的xml是:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/hyu_bg"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".KissingMeter" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="34dp"
        android:text="Tell Us Your Gender!"
        android:textSize="30dp" />

    <RadioGroup
        android:id="@+id/radioSex"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/gender_button"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="30dp" >

        <RadioButton
            android:id="@+id/radioMale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/radioFemale"
            android:layout_alignLeft="@+id/radioFemale"
            android:layout_marginBottom="34dp"
            android:checked="true"
            android:text="Iam a Guy!" />

        <RadioButton
            android:id="@+id/radioFemale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/gender_button"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="44dp"
            android:text="Iam a Girl!" />
    </RadioGroup>

    <Button
        android:id="@+id/gender_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/radioSex"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="18dp"
        android:text="Lol" />

</RelativeLayout>

首先,在这种情况下,不要依赖
视图的文本。如果您决定稍后更改它,则会产生问题。很可能,您不会更改它的
id
,所以只需使用它即可。把它改成这样

    btnDisplay.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) 
        {
           int selectedId = radioSexGroup.getCheckedRadioButtonId();  / get the id
           switch (selectedId)   // switch on the button selected
           {
                case R.id.radioMale:
                     Intent male_activity = new Intent(Gender.this, Meter.class);
                     startActivity(male_activity);
                     break;
                case R.id.radioFemale:
                    Intent male_activity = new Intent(Gender.this, For_Girl.class);
                    startActivity(male_activity);
                    break;
                default:
                  Toast.makeText(v.getContext(), "No gender selected", 
                                        Toast.LENGTH_SHORT).show();
                  break;
                }

            //********************************     

        }

    });