我是Android开发的新手。让我解释一下我的问题。我的主要活动中有一个复选框和一个按钮。当我选中(即启用)我的复选框并单击其下方的按钮时,我将移动到另一个活动,并且当我单击第二个活动中的按钮时,我会回到我的主活动。我的问题是当我回来时,复选框仍然未选中或禁用。即使从第二个活动回来后,我该怎么做才能保持其状态?请帮忙。
XML 布局的 CheckBox 部分是:
<CheckBox
android:id="@+id/ckBxAll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ON" />
主要活动的骨架代码:
Public class TestActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater mInflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
View mMainView = mInflater.inflate(R.layout.main, null);
setContentView(mMainView);
Enable_chkbox = (CheckBox)mMainView.findViewById(R.id.ckBxAll);
if(b_onResume==true)
{
Enable_chkbox.setChecked(b_onResume);
}
Enable_chkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
//do something
});//Enable_chkbox
Compose_btn=(Button)findViewById(R.id.btnCompose);
Compose_btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent gotoComposeMessage=new Intent(TestActivity.this,ComposeMessage.class);
startActivity(gotoComposeMessage);
}
});
}
@Override
protected void onResume() {
super.onResume();
b_onResume= Enable_chkbox.isChecked();
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
unregisterReceiver(myreceiver);
this.finish();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
moveTaskToBack(true);
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putBoolean("Enable_chkbox",true);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onRestoreInstanceState(savedInstanceState);
boolean myBoolean = savedInstanceState.getBoolean("Enable_chkbox");
}
}
我的第二个活动:
public class ComposeMessage extends Activity{
Button SaveSMS_btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.composemessage);
SaveSMS_btn=(Button)findViewById(R.id.btnSave);
SaveSMS_btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent gotoHome=new Intent(getApplicationContext(),BlockTestActivity.class);
Bundle BundleToCarryMessage = new Bundle();
EditText EnterSMS_edt=(EditText)findViewById(R.id.edtEnterSMS);
BundleToCarryMessage.putString("Message", EnterSMS_edt.getText().toString());
gotoHome.putExtras(BundleToCarryMessage);
startActivity(gotoHome);
}
});
}
}
第二个活动中使用的 XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/edtEnterSMS"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine" >
<requestFocus />
</EditText>
<Button
android:id="@+id/btnSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save" />
</LinearLayout>