有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java保存复选框的状态,然后加载它

我想做的是当按下主菜单上的一个按钮时,让设置菜单向上拉(设置菜单是作为独立于主菜单的活动实现的)。为了简单起见,假设我的主菜单是空的,只有一个按钮打开设置菜单。在设置菜单中,有一个复选框和一个返回到主活动的“完成”按钮

如何保存复选框并加载它(代码应该是什么,应该放在哪里,为什么等等)?我试着在谷歌上搜索,并试图复制结果,但我似乎无法得到它。到目前为止,发生了两件事:没有任何保存,或者我的程序崩溃

一旦我保存了有关复选框的信息,我如何才能从我的主要活动中访问这些信息@I want能够根据用户是否选中复选框来运行某些代码

我得到并尝试了一些结果: How to save the checkbox state? - 安卓 Saving Checkbox states

(请记住,我对这个完全陌生)

public class Settings extends ActionBarActivity {

    private static final String SETTING_CHECK_BOX = "SETTINGS";
    private CheckBox cb;
    //char boxChecked = '0';
    @Override
    protected void onCreate(Bundle savedSettings) {
        super.onCreate(savedSettings);
        cb = (CheckBox) findViewById(R.id.checkBox);
        setContentView(R.layout.activity_settings);
        cb.setChecked(isCheckedSettingEnabled());
    }

    private void setCheckedSettingEnabled(boolean enabled) {
        PreferenceManager.getDefaultSharedPreferences(this).edit().putBoolean(SETTING_CHECK_BOX, enabled).apply();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_settings, menu);
        return true;
    }

    private boolean isCheckedSettingEnabled() {
        return PreferenceManager.getDefaultSharedPreferences(this).getBoolean(SETTING_CHECK_BOX, false);
    }

    public void onPause() {
        super.onPause();

        // Persist the setting. Could also do this with an OnCheckedChangeListener.
        setCheckedSettingEnabled(cb.isChecked());
    }

    public void clickedDone (View v) {
        SharedPreferences settings = getSharedPreferences("SETTINGS", 0);
        settings.edit().putBoolean("check",true).commit();
        finish();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

因此,现在我的应用不再崩溃,但状态不会被记住(当“设置”菜单打开时,总是取消选中)。我换了cb。将已检查(检查状态)设置为cb。setChecked(TRUE),它没有改变任何东西(当设置菜单打开时,仍然始终未选中)。怎么回事


共 (1) 个答案

  1. # 1 楼答案

    使用onSaveInstanceState()存储状态数据后,系统使用onRestoreInstanceState(Bundle savedInstanceState)重新创建状态

    如果使用的是onSaveInstanceState(),则必须重写活动中的函数onRestoreInstanceState(),或使用onCreate()中的保存状态。我认为最好使用onRestoreInstanceState(),这可以减少onCreate()中的代码,因为只有在系统 关闭应用程序,为运行新应用程序腾出空间。如果只想保存选中状态,请使用共享首选项,无需onSaveInstanceState()。 //你在上课吗

    private  CheckBox cb;
    private SharedPreferences preferences ;
    private SharedPreferences.Editor editor;
    private boolean CHECKED_STATE;
        @Override
        protected void onCreate(Bundle savedSettings) {
            super.onCreate(savedSettings);
            setContentView(R.layout.activity_settings);
            preferences = getApplicationContext().getSharedPreferences("PROJECT_NAME",                 android.content.Context.MODE_PRIVATE);
            editor = preferences.edit();
            CHECKED_STATE = preferences.getBoolean("check", false);
            cb = (CheckBox) findViewById(R.id.checkBox);
            cb.setChecked(CHECKED_STATE);
            cb.setOnCheckedChangeListener(new OnCheckedChangeListener(){
             @Override
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
              editor.putBoolean("check", isChecked);
              editor.commit();
             }
            });
        }
    
       this code saves the state on clicking the check box.
    

    要使其在按Back Press时保存状态,请将以下内容添加到活动中

    @Override
        public void onBackPressed() {
            // TODO Auto-generated method stub
      editor.putBoolean("check", cb.isChecked());
                  editor.commit();
    }
    

    有关保存实例状态的更多详细信息,请参阅this link