我想知道为什么我的复选框不会将状态从 false 更改为 true。
我已经尝试从这里阅读如何设置复选框状态以及其他帖子,但我只是没有弄清楚我的代码有什么问题。
我的 Java 代码:
public class OrderActivity extends Activity implements OnClickListener{
public static String orderOne = null;
public static String orderTwo = null;
public static String orderThree = null;
public static String orderFour = null;
public static String orderFive = null;
public static String orderSix = null;
CheckBox chkChickenBreast, chkChickenWings, chkChickenTighs, chkWholeChicken, chkSpicyWings, chkChickenFillet;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order);
chkChickenBreast = (CheckBox) findViewById(R.id.check_box_order1);
chkChickenWings = (CheckBox) findViewById(R.id.check_box_order2);
chkChickenTighs = (CheckBox) findViewById(R.id.check_box_order3);
chkWholeChicken = (CheckBox) findViewById(R.id.check_box_order4);
chkSpicyWings = (CheckBox) findViewById(R.id.check_box_order5);
chkChickenFillet = (CheckBox) findViewById(R.id.check_box_order6);
Button checkOutOrder = (Button) findViewById(R.id.button_confirm_order);
checkOutOrder.setOnClickListener(this);
Button backToMain = (Button) findViewById(R.id.button_back_to_main);
backToMain.setOnClickListener(this);
}
@Override
public void onClick (View view) {
String[] orderedProduct = new String[6];
if (view.getId() == R.id.check_box_order1){
if (chkChickenBreast.isChecked()){
chkChickenBreast.setChecked(true);
orderedProduct[0] = "True";
}
else{
chkChickenBreast.setChecked(false);
orderedProduct[0] = "False";
}
}
if (view.getId() == R.id.button_confirm_order){
Intent goToReceipt = new Intent(this, ReceiptActivity.class);
if (chkChickenWings.isChecked() == true)
orderedProduct[1] = "True";
else
orderedProduct[1] = "False";
if (chkChickenTighs.isChecked() == true)
orderedProduct[2] = "True";
else
orderedProduct[2] = "False";
if (chkWholeChicken.isChecked() == true)
orderedProduct[3] = "True";
else
orderedProduct[3] = "False";
if (chkSpicyWings.isChecked() == true)
orderedProduct[4] = "True";
else
orderedProduct[4] = "False";
if (chkChickenFillet.isChecked() == true)
orderedProduct[5] = "True";
else
orderedProduct[5] = "False";
goToReceipt.putExtra(orderOne, orderedProduct[0]);
goToReceipt.putExtra(orderTwo, orderedProduct[1]);
goToReceipt.putExtra(orderThree, orderedProduct[2]);
goToReceipt.putExtra(orderFour, orderedProduct[3]);
goToReceipt.putExtra(orderFive, orderedProduct[4]);
goToReceipt.putExtra(orderSix, orderedProduct[5]);
startActivity(goToReceipt);
}
else if (view.getId() == R.id.button_back_to_main){
onBackPressed();
//Intent goToMenu = new Intent(this, MainActivity.class);
//startActivity(goToMenu);
}
}
@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;
}
}
我的 XML 代码是:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@layout/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center_horizontal"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/image_robinsons_logo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:contentDescription="@string/robinsons_picture_title"
android:src="@drawable/rsc" />
<ScrollView
android:id="@+id/scroll_order_product"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignLeft="@+id/image_robinsons_logo"
android:layout_below="@+id/image_robinsons_logo" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="0,1,2" >
<TableRow
android:id="@+id/table_row_title"
android:layout_width="fill_parent" >
<TextView
android:id="@+id/text_view_title_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textSize="12sp"
android:text="@string/row_title_name" />
<TextView
android:id="@+id/text_view_title_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textSize="12sp"
android:text="@string/row_title_price" />
<TextView
android:id="@+id/text_view_title_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textSize="12sp"
android:text="@string/row_title_image" />
</TableRow>
</TableLayout>
</ScrollView>
然后它转移到另一个活动:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_receipt);
Intent goToReceipt = getIntent();
String boolOrderOne = goToReceipt.getStringExtra(OrderActivity.orderOne);
if (boolOrderOne == "True"){
TextView orderOne = new TextView(this);
orderOne.setText(boolOrderOne);
setContentView(orderOne);
}
}