Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 条件中未执行复选框值_Java_Android_Xml - Fatal编程技术网

Java 条件中未执行复选框值

Java 条件中未执行复选框值,java,android,xml,Java,Android,Xml,我是andriod的新手,我正在尝试打印toast msg中的选中复选框值,但问题是如果条件未执行,但我给出了条件pizza.isSelected()要执行if循环,我的预期输出是所选项目pizza价格为100,但是我得到了选中的项目0 My MainActivity.java is package com.example.uicheckbox; import android.os.Bundle; import android.app.Activity; import android.v

我是andriod的新手,我正在尝试打印toast msg中的
选中复选框
值,但问题是
如果条件
未执行,但我给出了条件
pizza.isSelected()
要执行if循环,我的预期输出是
所选项目pizza价格为100
,但是我得到了
选中的项目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>
activity\u main.xml
使用

使用


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

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

@electrofant谢谢,现在应用程序工作成功了,我还需要一个帮助,如何打印两个以上选中的框总金额,例如:比萨饼价格100,咖啡的价格是70,但我需要在if条件之外定义toast。实际上,你的代码应该可以这样做。您在if语句中生成了字符串,随后显示了土司。@Electrofant感谢aloat,现在应用程序工作成功,我还需要一个帮助,如何打印两个以上的选定框总金额,例如:pizza price 100,coffee price 70,但我需要在if条件之外定义土司。实际上,您的代码应该可以正常工作。您在if语句中构建了字符串,并在其后显示toast。
if (pizza.isChecked()) {
    // ...
}