共享偏好布尔值

时间:2017-05-27 14:30:32

标签: android boolean sharedpreferences

我是初学者,我正试图使用​​布尔值来控制这个共享首选项方法。一个按钮用于更改值,第二个按钮用于读取值。但它说有一个(字符串,整数)的要求,我无法弄清楚我在哪里弄错了。你能帮忙吗?

public class MainActivity extends AppCompatActivity {

SharedPreferences preference;
SharedPreferences.Editor editor;
Button btn;
Button btn2;
Context context;
Boolean glowa;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btn2 = (Button) findViewById(R.id.btn2);
    btn = (Button) findViewById(R.id.btn);

    SharedPreferences preference = PreferenceManager.getDefaultSharedPreferences(this);
    boolean glowa = preference.getBoolean("GLOWA", false);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            editor.putBoolean("GLOWA", true);
            editor.apply();

        }
    });

    btn2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            SharedPreferences preference;
            SharedPreferences.Editor editor;
            preference = getBaseContext().getSharedPreferences("GLOWA");
            if (glowa == true) {
                Toast.makeText(MainActivity.this, "true", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(MainActivity.this, "mistake", Toast.LENGTH_LONG).show();
            }
        }
    });
}
}

3 个答案:

答案 0 :(得分:0)

更改此

preference = getBaseContext().getSharedPreferences("GLOWA");

SharedPreferences sharedpreferences = getSharedPreferences("GLOWA", Context.MODE_PRIVATE);
sharedpreferences.getBoolean("GLOWA",false); // return false if key not found in preference file

SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putBoolean("GLOWA", true);
editor.commit();       

您所需的首选项文件名与布尔值的键相同。最好选择两个不同的名字。

答案 1 :(得分:0)

要从SharedPreferences获取,请使用以下代码:

SharedPreference sharedPreferences = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
sharedPreferences.getBoolean("key",false);

要在SharedPreferences中存储数据,请使用以下代码:

SharedPreference sharedPreferences = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putBoolean("key", true);
editor.apply();

答案 2 :(得分:0)

我修复了你的类和sharedpreferences初始化。我希望这可以解决你的问题,从sharedpreference

获取布尔值
public class MainActivity extends AppCompatActivity {

    SharedPreferences preference;
    SharedPreferences.Editor editor;
    Button btn;
    Button btn2;
    Context context;
    Boolean glowa;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn2 = (Button) findViewById(R.id.btn2);
        btn = (Button) findViewById(R.id.btn);

        preference = this.getSharedPreferences("myAppPrefs", Context.MODE_PRIVATE);
        editor = sharedpreferences.edit();

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                editor.putBoolean("GLOWA", true);
                editor.apply();

            }
        });

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (preference.getBoolean("GLOWA",false)) {
                    Toast.makeText(MainActivity.this, "true", Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(MainActivity.this, "mistake", Toast.LENGTH_LONG).show();
                }
            }
        });
    }
}