Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.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 从Android中的其他活动获取复选框变量_Java_Android_Variables_Checkbox - Fatal编程技术网

Java 从Android中的其他活动获取复选框变量

Java 从Android中的其他活动获取复选框变量,java,android,variables,checkbox,Java,Android,Variables,Checkbox,我需要一些帮助来获取复选框的状态。基本上,我想知道是否选中了活动中的复选框,如果选中了,则在主活动上显示一些文本。我读了很多很多的问答,但都没有效果。我明白了: CheckBox show = (CheckBox) findViewById(R.id.checkBox); public CheckBox getShow() { return show; } 所以我会用 CheckBox setty = Settings.getCheck(); 另一节课上。但我有一个错误: 无法解析

我需要一些帮助来获取复选框的状态。基本上,我想知道是否选中了活动中的复选框,如果选中了,则在主活动上显示一些文本。我读了很多很多的问答,但都没有效果。我明白了:

CheckBox show = (CheckBox) findViewById(R.id.checkBox);

public CheckBox getShow() {
    return show;
}
所以我会用

CheckBox setty = Settings.getCheck();
另一节课上。但我有一个错误:

无法解析方法“getCheck()”


我该怎么做?我需要知道它是开还是关,并在主要活动中显示它。请记住,我不能使用意图,因为我没有按钮来转换,文本和值必须始终在主类上。

您可以使用共享首选项来存储复选框选中/取消选中值。根据该值,您可以在其他活动中显示文本

代码如下:

public static final String MyPREFERENCES = "MyPrefs";
在第一个活动的onCreate()方法中初始化共享首选项

 sharedPreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
  CheckBox check1 = (CheckBox) view.findViewById(R.id.your_checkbox_id);

  //now initialize listener for checkbox

   check1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putString("checked", "1");
                    editor.commit();
                } else {
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putString("checked", "0");
                    editor.commit();
                }
            }
        });
现在在另一个活动中,您只需加载共享首选项数据并根据条件显示消息

public void Load_checkbox() {
        //define MyPREFERENCES in other activity too- same as first activity
        SharedPreferences shared = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE); 
        if (shared.getString("checked", "").equals("1")) {
             Toast.makeText(getApplicationContext(), "CheckBox Checked " ,Toast.LENGTH_LONG).show();
        }else{
            Toast.makeText(getApplicationContext(), "CheckBox unchecked " ,Toast.LENGTH_LONG).show();
        }
    }

在第二个活动中调用此Load_checkbox()方法,您希望检查第一个活动的复选框状态

所有活动都是由意图启动的。因此,您可以通过将数据放入intent的额外字段,将数据从一个活动传递到另一个活动。如果您从一个活动返回,要传递结果,可以从初始活动开始“结果”活动。所以你不需要你的代码中显示的复选框的getter请阅读文章,主活动一开始就可能包含值true或false。我不能让用户打开另一个活动,只要他们想修改它@AdityaK
 sharedPreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
  CheckBox check1 = (CheckBox) view.findViewById(R.id.your_checkbox_id);

  //now initialize listener for checkbox

   check1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putString("checked", "1");
                    editor.commit();
                } else {
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putString("checked", "0");
                    editor.commit();
                }
            }
        });