如何检查来自另一个班级

时间:2017-01-26 19:07:50

标签: android togglebutton

 final Switch tButton = (Switch) findViewById(R.id.switch1);

        final SharedPreferences Switch = getSharedPreferences(PREFS,0);
        final SharedPreferences.Editor editor = Switch.edit();
        tButtonChecked = Switch.getBoolean("userMessage", false);
        tButton.setChecked(tButtonChecked);
        tButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    editor.putBoolean("userMessage", isChecked);
                    editor.commit();
                } else {
                    editor.putBoolean("userMessage", isChecked);
                    editor.commit();
                }
            }
        });

如何从另一个班级检查此切换按钮/开关按钮的状态(是否已选中)?

1 个答案:

答案 0 :(得分:0)

你已经到了一半。

在您的其他课程中,请执行以下操作:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
boolean isToggleButtonChecked = prefs.getBoolean("userMessage", false); // second argument is the value if the preference doesn't exist.