如果从Checkbox中忽略语句

时间:2013-11-05 18:34:00

标签: java android checkbox

我使用五个复选框制作披萨应用。复选框分别标记为Plain,Ex Cheese,Pepperoni,Green Peppers和Sausage。点击时还会显示一个按钮,根据点击的复选框显示构建的披萨的图片。

XML代码

<LinearLayout 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="#000000"
android:orientation="vertical"
tools:context=".MainActivity" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TableRow
            android:id="@+id/tablerow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <CheckBox
                android:id="@+id/checkbox1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="5"
                android:text="@string/Plain"
                android:textColor="#ffffff" />

            <CheckBox
                android:id="@+id/checkbox2"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="5"
                android:text="@string/Ex_Cheese"
                android:textColor="#ffffff" />
        </TableRow>

        <TableRow
            android:id="@+id/tablerow2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <CheckBox
                android:id="@+id/checkbox3"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="5"
                android:text="@string/Pepperoni"
                android:textColor="#ffffff" />

            <CheckBox
                android:id="@+id/checkbox4"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="5"
                android:text="@string/GPepper"
                android:textColor="#ffffff" />
        </TableRow>

        <TableRow
            android:id="@+id/tablerow3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <CheckBox
                android:id="@+id/checkbox5"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="5"
                android:text="@string/Sausage"
                android:textColor="#ffffff" />

            <Button
                android:id="@+id/Button1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="5"
                android:text="@string/Button1"
                android:textColor="#000000" 
                android:onClick="Order"/>
        </TableRow>
    </TableLayout>
</LinearLayout>

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:scaleType="fitXY" />

</LinearLayout>

Java代码     包com.example.pizza_app_v1;

public class MainActivity extends Activity {

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

/** Button Click event*/ 
public void Order (View view){
    CheckBox plain = (CheckBox) findViewById(R.id.checkbox1);
    CheckBox Ex_Cheese = (CheckBox) findViewById(R.id.checkbox2);
    CheckBox Pepperoni = (CheckBox) findViewById(R.id.checkbox3);
    CheckBox GreenPep = (CheckBox) findViewById(R.id.checkbox4);
    CheckBox Sausage = (CheckBox) findViewById(R.id.checkbox5);
    ImageView image = (ImageView) findViewById(R.id.imageView1);

    if (plain.isChecked()){
        Ex_Cheese.setChecked(false);
        Pepperoni.setChecked(false);
        GreenPep.setChecked(false);
        Sausage.setChecked(false);
        image.setImageResource(R.drawable.a_plain);
    }

    if (Pepperoni.isChecked() && Ex_Cheese.isChecked() == false){
        if(Pepperoni.isChecked() && GreenPep.isChecked() && Sausage.isChecked() == false ){
            image.setImageResource(R.drawable.f_pep_gp);
        } else if(Pepperoni.isChecked() && Sausage.isChecked() && GreenPep.isChecked() == false){
            image.setImageResource(R.drawable.c_pep);
        } else if(Pepperoni.isChecked() && GreenPep.isChecked() && Sausage.isChecked()){
            image.setImageResource(R.drawable.p_pep_gp_sau);
        } else {
            image.setImageResource(R.drawable.c_pep);
        }

    if(GreenPep.isChecked() && Ex_Cheese.isChecked() == false ){
        if(GreenPep.isChecked() && Sausage.isChecked() && Pepperoni.isChecked() == false ){
            image.setImageResource(R.drawable.g_gp_sau);
        } else{
            image.setImageResource(R.drawable.b_gp);
        }
    }

我遇到的问题是第二个if语句=&gt; if(GreenPep.isChecked()&amp;&amp; ...)被忽略。在应用程序中,如果选中Green Peppers,则不会出现图像。同样适用于检查青椒和香肠的情况。

1 个答案:

答案 0 :(得分:3)

您没有关闭前一个if语句的括号:

if (Pepperoni.isChecked() && Ex_Cheese.isChecked() == false){
    if(Pepperoni.isChecked() && GreenPep.isChecked() && Sausage.isChecked() == false ){
        image.setImageResource(R.drawable.f_pep_gp);
    } else if(Pepperoni.isChecked() && Sausage.isChecked() && GreenPep.isChecked() == false){
        image.setImageResource(R.drawable.c_pep);
    } else if(Pepperoni.isChecked() && GreenPep.isChecked() && Sausage.isChecked()){
        image.setImageResource(R.drawable.p_pep_gp_sau);
    } else {
        image.setImageResource(R.drawable.c_pep);
    }