0

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

在此处输入图像描述

  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>
4

2 回答 2

3

利用

if (pizza.isChecked()) {
    // ...
}
于 2013-09-08T18:50:09.603 回答
1

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

于 2013-09-08T19:07:15.933 回答