复选框值未在条件中执行

时间:2013-09-08 18:42:56

标签: java android xml

我是andriod的新手,我正在尝试在toast msg中打印selected checkbox值,但问题是if condition没有被执行,但是我给了条件pizza.isSelected()来执行if循环,我的预期输出为selected item pizza price is 100,但我得到了selected item 0

enter image description here

  My MainActivity.java is

package com.example.uicheckbox;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
import android.support.v4.app.NavUtils;

public class MainActivity extends Activity {
public CheckBox pizza,coffee,burger;
public Button buttonsubmit;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonClick();
}
public void buttonClick() {
pizza=(CheckBox)findViewById(R.id.checkBox1);
coffee=(CheckBox)findViewById(R.id.checkBox2);
burger=(CheckBox)findViewById(R.id.checkBox3);
buttonsubmit=(Button)findViewById(R.id.button1);
buttonsubmit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
StringBuilder builder=new StringBuilder();
builder.append("Selected Items");
int totalamount =0;
if(pizza.isSelected())
{
System.out.println("pizza");
totalamount+= 100;
builder.append("Pizza price is");
}
if(coffee.isSelected())
{
System.out.println("coffee");
totalamount+= 70;
builder.append("Cofee price is");
}
if(burger.isSelected())
{
System.out.println("burger");
totalamount+= 120;
builder.append("Burger price is");
}
builder.append(totalamount);
Toast.makeText(getApplicationContext(),builder.toString(), Toast.LENGTH_LONG).show();
}
});
}
}

我的此活动的xml文件是

activity_main.xml

<RelativeLayout 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" >

<CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:text="@string/menu1" />

<CheckBox
    android:id="@+id/checkBox2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/checkBox1"
    android:layout_below="@+id/checkBox1"
    android:layout_marginTop="52dp"
    android:text="@string/menu2" />

<CheckBox
    android:id="@+id/checkBox3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/checkBox2"
    android:layout_below="@+id/checkBox2"
    android:layout_marginTop="42dp"
    android:text="@string/menu3" />

 <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="87dp"
    android:layout_toLeftOf="@+id/checkBox3"
    android:text="@string/order" />

 </RelativeLayout>

2 个答案:

答案 0 :(得分:3)

使用

if (pizza.isChecked()) {
    // ...
}

答案 1 :(得分:1)

对于复选框,它是缺血的而不是被选中的