【问题标题】:How to maintain CheckBox state across the activities如何在活动中维护 CheckBox 状态
【发布时间】:2014-09-01 15:36:15
【问题描述】:

如何在活动中maintain CheckBox state

我在FirstActivity.java 中检查了一个复选框,但是当我从任何其他活动中move backFirstActivity.java 时,比如说从SecondActivity.java..

那么,我是not getting my CheckBox as check,我之前检查过。

使用onSaveInstanceState & onRestoreInstanceState,完整代码如下:-

public class FirstActivity extends Activity {

    CheckBox cb1;
    Button btnNext;
    private boolean boolCheck1 = false;

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

        cb_cb1 = (CheckBox) findViewById(R.id.cb1);

        btnNext = (Button) findViewById(R.id.btnNext);

        // control status of body frame checkboxes
        cb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

           @Override
           public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                if(isChecked){

                }
            }
        });

        btnNext.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                    Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
                    startActivity(intent);
            }
        });
    }

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

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

【问题讨论】:

  • 投反对票不是问题,但你也应该写下理由

标签: java android checkbox


【解决方案1】:

您最好的选择是使用sharedPreferences

checkbox 所在的活动中,在onCreate() 中输入以下内容:

SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
Editor editor = pref.edit();

然后,在您的 intent 中转到另一个活动,或“保存”button,或任何您想要的,使用以下内容:

editor.putBoolean("checkbox", yourCheckbox.isChecked()); 
editor.commit(); // commit changes

现在,设置已保存。

当你回到你的活动时,在你的onCreate中使用它:

if (pref.getBoolean("checkbox", false) == true){ //false is default value
yourCheckbox.setChecked(true); //it was checked
} else{
yourCheckbox.setChecked(false); //it was NOT checked
}

您可以阅读更多here

希望这有帮助

【讨论】:

  • 我的荣幸!很高兴它有帮助:-)
【解决方案2】:

使用SharedPreferencesSQLiteDatabase存储复选框的值,从DatabaseSharedPreference获取后即可使用。

如果您使用onSaveInstanceStateonRestoreInstanceState,当用户关闭应用并且应用不再位于Memory 中时,所有引用都将被清除。

【讨论】:

    【解决方案3】:

    要跨活动维护状态,您必须使用 SharedPreferences 或数据库。在你的情况下SharedPreferences 是最好的选择

    您使用了 OnSavedInstance,它只会保存实例,直到 Activity 未被销毁,即它保留在堆栈上。

    查看activity lifecycle 以了解有关活动何时被销毁的更多信息。

    对于每个复选框,您可以将其值存储在 sharedPreference 和 on onCreate() 你应该根据你的活动检查复选框 到 sharedPreference 值

    这里是你如何使用共享首选项

    SharedPreferences prefs= context.getSharedPreferences("myPrefs", 0);
         SharedPreferences.Editor editor = prefs.edit();
            editor.putBoolean("checkbox1", true);//true if checked 
            editor.commit();
          // get stored value , if no value is stored then assume its false
         prefs.getBoolean("checkbox1", false);
    

    【讨论】:

      猜你喜欢
      • 2014-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-22
      • 1970-01-01
      • 2021-10-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多