0

我是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>
4

3 回答 3

5

还有另一种方法。您需要覆盖 onSaveInstanceState(Bundle savedInstanceState) 并将要更改的应用程序状态值写入 Bundle 参数,如下所示:

下一个代码用于在移动到第二个活动之前保存复选框状态:

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {

  savedInstanceState.putBoolean("MyCheckBox", enable);

  super.onSaveInstanceState(savedInstanceState);
}

这是当您恢复复选框状态时:

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
  super.onRestoreInstanceState(savedInstanceState);

  boolean myBoolean = savedInstanceState.getBoolean("MyCheckBox");

}

编辑:

public class TestActivity extends Activity implements OnCheckedChangeListener, OnClickListener  {

        private CheckBox Enable_chkbox;
        private View mMainView;
        private Button Compose_btn;
        private boolean myBoolean = false;;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            LayoutInflater mInflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
            mMainView = mInflater.inflate(R.layout.main, null);        
            setContentView(mMainView);
            initView();
        }

        private void initView(){

             Enable_chkbox = (CheckBox)mMainView.findViewById(R.id.ckBxAll);
             Enable_chkbox.setOnCheckedChangeListener(this);
             Compose_btn=(Button)mMainView.findViewById(R.id.btnCompose);
             Compose_btn.setOnClickListener(this);
        }

        @Override
        protected void onResume() {
          super.onResume();
          Enable_chkbox.setChecked(myBoolean);

        }

        @Override
        public void onSaveInstanceState(Bundle savedInstanceState) {
           super.onSaveInstanceState(savedInstanceState);
           savedInstanceState.putBoolean("Enable_chkbox", Enable_chkbox.isChecked());      
        }

        @Override
        protected void onRestoreInstanceState(Bundle savedInstanceState) {
            super.onRestoreInstanceState(savedInstanceState);
            myBoolean = savedInstanceState.getBoolean("Enable_chkbox");
        }

        @Override
        public void onClick(View arg0) {
            //do something
        }

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            //do something
        }  
}

'

希望能帮助到你。

于 2012-05-16T07:07:28.287 回答
2

出于所有一般目的,您必须在离开活动时保存复选框和单选按钮的状态,并在返回活动时恢复它们。
为此,我们通常使用共享偏好。我们在方法中保存按钮的状态,在onPause()方法中恢复状态onResume()
要使用共享首选项,您可以执行以下操作:

SharedPreferences prefs = this.getSharedPreferences(
      "com.example.app", Context.MODE_PRIVATE);

阅读偏好:

String dateTimeKey = "com.example.app.datetime";

// use a default value using new Date()
long l = prefs.getLong(dateTimeKey, new Date().getTime()); 

编辑和保存首选项

Date dt = getSomeDate();
prefs.edit().putLong(dateTimeKey, dt.getTime()).commit();

在此博客上查看更多示例。
将上述代码放置在正确的方法(onPause 和 onResume)中,您可以恢复按钮的状态。

于 2012-05-16T06:56:21.940 回答
0

使用 startActivityforResult 切换到第二个活动。当您使用它切换时,您没有完成当前活动,因此它存储了活动的状态。

于 2012-05-16T06:49:52.270 回答