Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/216.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.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,我正在Android Studio中编写我的第一个应用程序。用户应选择训练类型(肌肉增加、体重减轻、自己的计划),然后选择性别(男性/女性)。因此,6个可能的结果和2个相应的无线电组。在选择必要的选项后,用户点击确认按钮,进入培训类型的必要屏幕(使用6种addListeneronbutton方法之一)。但安装后,应用程序不会做出反应。 你能告诉我我做错了什么吗 public void onRadioButtonClicked(View view) { switch (view.getId

我正在Android Studio中编写我的第一个应用程序。用户应选择训练类型(肌肉增加、体重减轻、自己的计划),然后选择性别(男性/女性)。因此,6个可能的结果和2个相应的无线电组。在选择必要的选项后,用户点击确认按钮,进入培训类型的必要屏幕(使用6种addListeneronbutton方法之一)。但安装后,应用程序不会做出反应。 你能告诉我我做错了什么吗

public void onRadioButtonClicked(View view) {
    switch (view.getId()) {
        case R.id.Muscles:
                switch (view.getId()){
                    case R.id.Male:
                        addListenerOnButton();
                    case R.id.Female:
                        addListenerOnButton2();
                }
                break;
        case R.id.Diet:
            switch (view.getId()){
                case R.id.Male:
                    addListenerOnButton3();
                case R.id.Female:
                    addListenerOnButton4();
            }
            break;
        case R.id.Own:
            switch (view.getId()){
                case R.id.Male:
                    addListenerOnButton3();
                case R.id.Female:
                    addListenerOnButton4();
            }
            break;
    }
}
相应的xml文件具有以下用于选择性别的代码:

<RadioGroup
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:id="@+id/radioGroup">
    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/Male"
        android:text="Male"
        android:onClick="onRadioButtonClicked"/>
    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:id="@+id/Female"
        android:text="Female"
        android:onclick="onRadioButtonClicked"/>
</RadioGroup>

在同一个xml第二个无线电组中选择培训类型:

    <TableRow
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/radioGroup2">
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:id="@+id/Muscles"
            android:text="@string/Muscles"
            android:onClick="onRadioButtonClicked"/>
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/Diet"
            android:text="@string/Fat"
            android:onClick="onRadioButtonClicked"/>
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/Own"
            android:text="@string/Own"
            android:onClick="onRadioButtonClicked"/>
    </RadioGroup>

您正在做的事情远比必要的复杂。你需要的是两个独立的广播组。不要在单击每个单选按钮时执行代码,而是应该在按钮上显示事件

当用户单击按钮时,应检查两个字段是否都已检查,然后在下一屏幕上传递这两个值。然后,您将处理解释用户输入的问题

因此,解决方案很简单

public int onRadioButtonClicked (View view){
    return view.getId();
}

public int onRadioButtonClicked2 (View view2){
    return view2.getId();
}
然后,我不知道如何得到比嵌套语句更简单的方法,比如:

Button button;


public void addListenerOnButton() {
    final Context context = this;
    button = (Button) findViewById(R.id.button5);

    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            switch(onRadioButtonClicked2(arg0)) {
                case R.id.Muscles:
                    switch (onRadioButtonClicked(arg0)) {
                        case R.id.Male:
                            Intent intent1 = new Intent(context, MaleMuscules.class);
                            startActivity(intent1);
                            break;
                        case R.id.Female:
                            Intent intent2 = new Intent(context, FemaleMuscules.class);
                            startActivity(intent2);
                            break;
                    }
                    break;
                case R.id.Diet:
                    switch (onRadioButtonClicked(arg0)) {
                        case R.id.Male:
                            Intent intent3 = new Intent(context, MaleLoss.class);
                            startActivity(intent3);
                            break;
                        case R.id.Female:
                            Intent intent4 = new Intent(context, FemaleLoss.class);
                            startActivity(intent4);
                            break;
                    }
                    break;
                case R.id.Own:
                    switch (onRadioButtonClicked(arg0))
                    {
                        case R.id.Male:
                            Intent intent5 = new Intent(context, MaleOwn.class);
                            startActivity(intent5);
                            break;
                        case R.id.Female:
                            Intent intent6 = new Intent(context, FemaleOwn.class);
                            startActivity(intent6);
                            break;
                    }
                  break;
            }
        }

    });

}

为什么是嵌套组?难道你的应用程序不能仅仅根据两个独立广播组的值来计算吗?Ramsay,我认为这会有所改进,但事实并非如此。当您“检查两个字段是否都被选中,然后在下一个屏幕上检查pas值”时,请键入代码的典型外观。我已将onClick分为两部分: