Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.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:如何根据spinner&;的用户输入执行if-else语句;单选按钮_Android - Fatal编程技术网

Android:如何根据spinner&;的用户输入执行if-else语句;单选按钮

Android:如何根据spinner&;的用户输入执行if-else语句;单选按钮,android,Android,我试图根据用户单击的内容编写if-else语句。这是我的main.xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:text

我试图根据用户单击的内容编写if-else语句。这是我的main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<TextView 
android:text="Food #1:" 
android:textSize="20dp" 
android:textStyle="bold"
android:layout_height="wrap_content" 
android:id="@+id/txt_comparing" 
android:layout_width="wrap_content"
android:layout_centerHorizontal="true">
</TextView>

<Spinner 
android:layout_height="wrap_content" 
android:layout_width="match_parent"
android:layout_below="@+id/txt_comparing"
android:id="@+id/spn_1">
</Spinner>

    <RadioGroup
android:id="@+id/canteen1"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/spn_1" >

<RadioButton
    android:id="@+id/option_0"
    android:text="ITAS  "/>
<RadioButton
    android:id="@+id/option_1"
    android:text="Design "/>
<RadioButton
    android:id="@+id/option_2"
    android:text="Engineering "/>

</RadioGroup>

<TextView 
android:text="Food #2:" 
android:textSize="20dp" 
android:layout_centerHorizontal="true"
android:textStyle="bold"
android:layout_below="@+id/canteen1"
android:layout_height="wrap_content" 
android:id="@+id/txt_and" 
android:layout_width="wrap_content">
</TextView>

<Spinner 
android:layout_height="wrap_content" 
android:layout_width="match_parent" 
android:layout_below="@+id/txt_and"
android:id="@+id/spn_2">
</Spinner>

<RadioGroup
android:id="@+id/canteen2"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/spn_2" >

<RadioButton
    android:id="@+id/option1_0"
    android:text="ITAS  "/>
<RadioButton
    android:id="@+id/option1_1"
    android:text="Design "/>
<RadioButton
    android:id="@+id/option1_2"
    android:text="Engineering "/>

</RadioGroup>

<Button 
android:id="@+id/btn_compare" 
android:text="   Compare   " 
android:textStyle="bold"
android:layout_alignParentBottom="true" 
android:layout_height="wrap_content" 
android:layout_width="wrap_content"
android:layout_alignParentRight="true">>
</Button>

<Button 
android:text="  Clear   " 
android:layout_height="wrap_content" 
android:id="@+id/clear" 
android:layout_toLeftOf="@+id/btn_compare" 
android:layout_alignTop="@+id/btn_compare" 
android:layout_alignBottom="@+id/btn_compare" 
android:layout_width="wrap_content">
</Button>

>
在我的主java类中,如何根据用户单击的内容创建if-else语句?我已经尝试和研究了很多次,但没有得到答案


提前谢谢你

您应该将OnClickListener分配给每个可单击视图(按钮)。
每个按钮可以是同一个侦听器,也可以是新的不同实例。

对于单个侦听器,您可以实现按钮id切换器,并知道按下了哪个按钮。
我花了一点时间准备插图:

OnClickListener listener = new OnClickListener() {

    @Override
    public void onClick(View view) {
    switch (view.getId()) {
            case R.id.button1:
                Toast.makeText(getApplicationContext(), "Button 1 pressed", Toast.LENGTH_SHORT).show();
            break;
            case R.id.button2:
                Toast.makeText(getApplicationContext(), "Button 2 pressed", Toast.LENGTH_SHORT).show();
            break;
        }

    }
};

((Button)findViewById(R.id.button1)).setOnClickListener(listener);
((Button)findViewById(R.id.button2)).setOnClickListener(listener);

那纺纱机呢?因为我试过你给我的密码,但没用。它只显示用户通过单选按钮选择的内容。但是,如果用户从微调器中选择一个项目,从radiobutton中选择一个选项,则if-else语句将是?不幸的是,我不能代替你编写应用程序。在onClick方法中,您应该分析所有需要的控件的状态。