gpt4 book ai didi

java - 复选框中的 If 语句被忽略

转载 作者:行者123 更新时间:2023-12-01 23:27:18 25 4
gpt4 key购买 nike

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

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

最佳答案

您没有关闭上一个 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);
}

关于java - 复选框中的 If 语句被忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19796199/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com