如何在共享首选项中存储toogle按钮状态并稍后在android中加载状态? [英] How to Store toogle button status in shared preference and load the status later in android?

查看:87
本文介绍了如何在共享首选项中存储toogle按钮状态并稍后在android中加载状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是我想以共享首选项存储我的太极按钮状态,以便当我返回到应用程序时,我的太极按钮将保持在以前的状态。 您想使用它吗?。如果用户启用按钮,则意味着当他返回按钮时它将显示该按钮将启用。到目前为止,我所做的是

The thing is I want to store my toogle button status in shared preference, so that I when I return to the application my toogle button wil stay in previous state. This is kind of something Do you want to use this. If user make the button enable that means when he return it will show the button will enable. So far I did is

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类

And this is my 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, "");
    }


    }

如何创建我的共享首选项类以存储数据并在以后使用?

So how to create my shared preference class for storing the data and use it later?

推荐答案

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

you can use SharedPreferences to store state of your ToggleButton


SharedPreferences 类提供了一个通用框架,使您可以保存和检索原始数据类型的持久键值对。您可以使用SharedPreferences保存任何原始数据:布尔值,浮点数,整数,长型和字符串。这些数据将在用户会话期间保持持久(即使您的应用程序被终止)。

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

这篇关于如何在共享首选项中存储toogle按钮状态并稍后在android中加载状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆