如何在共享首选项中存储切换按钮状态并稍后在 android 中加载状态?

How to Store toogle button status in shared preference and load the status later in android?

问题是我想在共享首选项中存储我的 toogle 按钮状态,这样当我 return 到应用程序时,我的 toogle 按钮将保持以前的状态。这有点像 你想用这个吗? 如果用户启用按钮,这意味着当他 return 时它会显示按钮将启用。到目前为止我做的是

tb_vibrate = (ToggleButton)this.findViewById(R.id.tb_vibrate);
tb_vibrate.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            if(tb_vibrate.isChecked())
            {
                Toast.makeText(ProfileActivity.this, "Toggle button is on", Toast.LENGTH_LONG).show();
                tb_vibrate.setChecked(true);
            }
            else {
                Toast.makeText(ProfileActivity.this, "Toggle button is Off", Toast.LENGTH_LONG).show();
                tb_vibrate.setChecked(false);
            }
        }
    });

这是我的 SharePreference class

public class NotificationManager {

    private static final String PREFS_FILE_NAME = "AppNotificationManager";
    private static final String VIBRATE = "vibrate";
    private static final String NOTIFICATION_ALERT = "notification_alert";
    private static final String NOTIFICATION_SOUND = "notification_sound";
    private static final String CALL_RINGTONE = "call_ringtone";
    private static final String RINGTONE_VIBRATION = "ringtone_vibrate";

    public static void setVibrate(final Context ctx, final String vibrate) {
        final SharedPreferences prefs = ctx.getSharedPreferences(NotificationManager.PREFS_FILE_NAME, Context.MODE_PRIVATE);
        final SharedPreferences.Editor editor = prefs.edit();
        editor.putString(NotificationManager.VIBRATE, vibrate);
        editor.commit();
    }

    public static String getVibrate(final Context ctx) {
        return ctx.getSharedPreferences(NotificationManager.PREFS_FILE_NAME,
                Context.MODE_PRIVATE).getString(NotificationManager.VIBRATE, "");
    }


    }

那么如何创建我的共享首选项 class 来存储数据并在以后使用它?

您可以使用 SharedPreferences 来存储您的 ToggleButton

的状态

The SharedPreferences class provides a general framework that allows you to save and retrieve persistent key-value pairs of primitive data types. You can use SharedPreferences to save any primitive data: booleans, floats, ints, longs, and strings. This data will persist across user sessions (even if your application is killed).

示例代码

SharedPreferences sp=getSharedPreferences("Login", Context.MODE_PRIVATE);
SharedPreferences.Editor Ed=sp.edit()

tb_vibrate = (ToggleButton)this.findViewById(R.id.tb_vibrate);
tb_vibrate.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            if(tb_vibrate.isChecked())
            {
                Toast.makeText(ProfileActivity.this, "Toggle button is on", Toast.LENGTH_LONG).show();
                tb_vibrate.setChecked(true);

                Ed.putBoolean("ISCHECKED",true);
                Ed.commit();

            }
            else {
                Toast.makeText(ProfileActivity.this, "Toggle button is Off", Toast.LENGTH_LONG).show();
                tb_vibrate.setChecked(false);
                Ed.putBoolean("ISCHECKED",false);
                Ed.commit();
            }
        }
    });

获取值使用下面的代码

SharedPreferences preferences = getSharedPreferences("MyPrefs", MODE_PRIVATE);
 boolean flag = preferences.getBoolean("ISCHECKED", false);
 if(flag){
        tb_vibrate.setChecked(true);
    }else {
        tb_vibrate.setChecked(false);
    }