[英]using intent to save which checkboxes were checked in different activity
So in one activity I have a button . 因此,在一项活动中,我有一个按钮。
clicking that button will initialize a different activity which has a list view , each view there contains list of checkboxes the user should choose from 单击该按钮将初始化另一个具有列表视图的活动,该视图中的每个视图都包含用户应从中选择的复选框列表
(sort of like questions and multiple answers...) (有点像问题和多个答案...)
thing is, when going back to previous activity I want the values of those checkboxes to be saved . 问题是,当返回上一个活动时,我希望保存这些复选框的值。 I'm thinking intents are the way to do it but I'm not so sure how...
我在想用意图来做到这一点,但我不确定如何...
Start Activity B by using startActiviyForResult()
in your button click listener. 通过启动活动B
startActiviyForResult()
在您的按钮点击收听。
Button btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this);
startActivityForResult(intent, 1234); // 1234 is used in callback
}
});
In second activity: 在第二个活动中:
For return data using back button, override onBackPressed()
对于使用后退按钮返回数据,覆盖
onBackPressed()
@Override
public void onBackPressed() {
Intent returnData = new Intent();
// get data from checkboxes
returnData.putExtra("key_1", data1);
returnData.putExtra("key_2", data2);
setResult(MainActivity.RESULT_OK, returnData);
super.onBackPressed();
}
For return data using normal button, use something like this: 对于使用普通按钮的返回数据,请使用如下所示的内容:
exitbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent returnData = new Intent();
// get data from checkboxes
returnData.putExtra("key_1", data1);
returnData.putExtra("key_2", data2);
setResult(MainActivity.RESULT_OK, returnData);
finish();
}
});
If you want cancel second activity, setResult(RESULT_CANCELED)
, instead RESULT_OK. 如果你想取消第二个活动,
setResult(RESULT_CANCELED)
而不是RESULT_OK。
Then in MainActivity override onActivityResult()
, it is the callback when second activity finishes. 然后在MainActivity重写
onActivityResult()
它是回调当第二活动结束。
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1234) { // previous defined
// Make sure the request was successful
if (resultCode == RESULT_OK) {
int data1 = data.getIntExtra("key_1", 0);
int data2 = data.getIntExtra("key_2", 0);
}
}
}
in your activity A : 在您的活动A中:
start your activity B with startActivityForResult : 使用startActivityForResult开始您的活动B:
Intent intent = new Intent(ActivityA.this,ActivityB.class);
startActivityForResult(intent, 1); // intent and requestCode of 1
in your activity B : get your checkBox values : 在活动B中:获取您的复选框值:
boolean isChecked = ((CheckBox) findViewById(R.id.checkBox1)).isChecked(); // your check box value for example
then send your values back to the first activity : 然后将您的值发送回第一个活动:
public static final String FIRSTCHECKBOX = "first";
Intent returnIntent = new Intent();
returnIntent.putExtra(FIRSTCHECKBOX, isChecked); // add all your check box values like this
setResult(RESULT_OK, returnIntent);
finish();
if you want cancel it (such as user click on cancel btn) : 如果你想取消它(如取消BTN用户点击):
Intent returnIntent = new Intent();
setResult(RESULT_CANCELED, returnIntent);
finish();
again in your activity A : 再次在您的活动A中:
get your values : 得到你的价值观:
protected void onActivityResult(int requestCode, int resultCode, Intent resultIntent) {
if (resultCode == RESULT_OK) {
if (requestCode ==1) {
Bolean check1 =resultIntent.getBooleanExtra(FIRSTCHECKBOX,0)// do it for all other check boxes - 0 is defualt value that you can choose 0 or 1
}
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.