0

我正在使用五个复选框来制作比萨应用程序。复选框标记为原味、前奶酪、意大利辣香肠、青椒和香肠。还有一个按钮,单击该按钮将根据单击的复选框显示构建的比萨饼的图片。

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 statement=> if(GreenPep.isChecked() &&...) 被忽略了。在应用程序中,如果选中 Green Peppers,则不会出现任何图像。检查青椒和香肠时也是如此。

4

1 回答 1

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);
    }
于 2013-11-05T18:39:01.740 回答