我想知道为什么我的复选框不会将状态从false更改为true。
我已经尝试过阅读how to set check box state以及其他帖子,但我还没弄清楚我的代码有什么问题。
我的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);
}
}
答案 0 :(得分:0)
请尝试以下表单:
oncreate中的代码段代码:
check = (CheckBox) findViewById(R.id.checkBox);
// get state of checkbox
boolean isChecked = getBooleanFromPreferences("isChecked");
// set state checkbox
check.setChecked(isChecked);
// invoked if status change
check.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton view, boolean isChecked) {
CheckInActivity.this.putBooleanInPreferences(isChecked,
"isChecked");
}
});
check.setOnClickListener(isCheckBoxClicked);
方法:
public void putBooleanInPreferences(boolean isChecked, String key) {
SharedPreferences sp = this.getPreferences(Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putBoolean(key, isChecked);
editor.commit();
}
public boolean getBooleanFromPreferences(String key) {
SharedPreferences sp = this.getPreferences(Activity.MODE_PRIVATE);
Boolean isChecked = sp.getBoolean(key, false);
return isChecked;
}
// if state of checkbox is true
private OnClickListener isCheckBoxClicked = new OnClickListener() {
public void onClick(View v) {
if ((check.isChecked())) {
// Your code if checkbox state is true
} else {
}
}
};
也许它可以帮助您尝试其他解决方案。