作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用五个复选框来制作披萨应用程序。复选框标记为原味、奶酪、意大利辣香肠、青椒和香肠。还有一个按钮,单击该按钮时将根据单击的复选框显示构建的披萨的图片。
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/
我是一名优秀的程序员,十分优秀!