Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在活动中维护复选框状态_Java_Android_Checkbox - Fatal编程技术网

Java 如何在活动中维护复选框状态

Java 如何在活动中维护复选框状态,java,android,checkbox,Java,Android,Checkbox,如何在活动中维护复选框状态 我在FirstActivity.java中选中了一个复选框,但当我从任何其他活动(例如从SecondActivity.java)移回FirstActivity.java时 然后,我没有将我的复选框作为我之前选中的复选框 使用onSaveInstanceState和onRestoreInstanceState,请参阅下面的完整代码:- public class FirstActivity extends Activity { CheckBox cb1;

如何在活动中维护复选框状态

我在FirstActivity.java中选中了一个复选框,但当我从任何其他活动(例如从SecondActivity.java)移回FirstActivity.java时

然后,我没有将我的复选框作为我之前选中的复选框

使用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");
    }
}

要跨活动维护状态,必须使用SharedReferences或数据库。对你来说,这是最好的选择

您已使用OnSavedInstance,它将只保存实例,直到活动未被销毁,即它保留在堆栈上

检查以了解有关活动何时被销毁的更多信息

对于每个复选框,您可以将其值存储在SharedReference和on中 创建活动后,应根据以下内容选中复选框: 指向SharedReference值

下面是如何使用共享pref

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);

要跨活动维护状态,必须使用SharedReferences或数据库。对你来说,这是最好的选择

您已使用OnSavedInstance,它将只保存实例,直到活动未被销毁,即它保留在堆栈上

检查以了解有关活动何时被销毁的更多信息

对于每个复选框,您可以将其值存储在SharedReference和on中 创建活动后,应根据以下内容选中复选框: 指向SharedReference值

下面是如何使用共享pref

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);
使用或存储复选框的值,您可以在从数据库或SharedReference获取后使用它

如果使用onSaveInstanceState&onRestoreInstanceState,当用户关闭应用程序且应用程序不再位于内存中时,所有引用都将清除

使用或存储复选框的值,您可以在从数据库或SharedReference获取后使用它

如果使用onSaveInstanceState&onRestoreInstanceState,当用户关闭应用程序且应用程序不再位于内存中时,所有引用都将清除


您最好的选择是使用SharedReferences

在复选框所在的活动中,在onCreate中输入以下内容:

SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
Editor editor = pref.edit();
editor.putBoolean("checkbox", yourCheckbox.isChecked()); 
editor.commit(); // commit changes
然后,在您想要转到另一个活动、保存按钮或任何您想要的内容时,请使用以下命令:

SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
Editor editor = pref.edit();
editor.putBoolean("checkbox", yourCheckbox.isChecked()); 
editor.commit(); // commit changes
现在,设置已保存

当您回到活动中时,请在onCreate中使用此选项:

你可以读更多


希望这有帮助

您最好的选择是使用SharedReference

在复选框所在的活动中,在onCreate中输入以下内容:

SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
Editor editor = pref.edit();
editor.putBoolean("checkbox", yourCheckbox.isChecked()); 
editor.commit(); // commit changes
然后,在您想要转到另一个活动、保存按钮或任何您想要的内容时,请使用以下命令:

SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
Editor editor = pref.edit();
editor.putBoolean("checkbox", yourCheckbox.isChecked()); 
editor.commit(); // commit changes
现在,设置已保存

当您回到活动中时,请在onCreate中使用此选项:

你可以读更多


希望这有助于

否决不是问题,但你也应该写理由否决不是问题,但你也应该写理由我很高兴!很高兴能帮上忙:-我的荣幸!很高兴它有帮助:-