如何在 SharedPreferences 中保存我的应用程序背景? [英] How to save my app background in SharedPreferences?

查看:46
本文介绍了如何在 SharedPreferences 中保存我的应用程序背景?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有按钮的片段,可以为整个应用程序设置背景主题.我已经设置了一个界面,因此片段可以告诉主要活动设置背景或根据用户单击的按钮将其删除.

I have a fragment with a button that sets a background theme for the whole app. I have set up an interface so the fragment can tell the main activity to set the background or remove it depending on what button the user clicks.

问题在于,每次打开应用程序时,后台都不会保存,需要再次切换.我已经看到这可以通过 SharedPreferences 解决,但在这里实现它让我感到困惑

在我的片段中这提供了两个按钮,将值 1 或 2 发送到主活动以切换背景

In my fragment This presents two buttons that send the values 1 or 2 to the main activity to toggle the background

enable = (Button) rootView.findViewById(R.id.enable);
enable.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            listener.themechanged(2);
            enable.setVisibility(View.GONE);
            disable.setVisibility(View.VISIBLE);

        }
    });
    disable = (Button) rootView.findViewById(R.id.disable);
    disable.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            listener.themechanged(1);
            disable.setVisibility(View.GONE);
            enable.setVisibility(View.VISIBLE);
        }
    });

在我的主要活动中这会从侦听器中获取值并根据值切换背景

In my Main Activity This takes the value from the listener and toggles the background depending on what the value is

    @Override

    public void themechanged(int value) {

    if(value==2) {
        if (isDarkTheme) {
            appbackground.setVisibility(View.GONE);
            shade.setVisibility(View.GONE);
        } else {
            appbackground.setVisibility(View.VISIBLE);
            shade.setVisibility(View.VISIBLE);
        }
    }else if(value!=2||value==1){
            appbackground.setVisibility(View.GONE);
            shade.setVisibility(View.GONE);
    }
}

推荐答案

使用 SharedPrefence 来存储主题的值,例如-:

Use SharedPrefence to store the value for theme like-:

全局变量

SharedPreferences pref;
SharedPreferences.Editor editor;

OnCreateView()

pref = getActivity().getSharedPreferences("Theme", Context.MODE_PRIVATE);
editor = pref.edit();

现在,将 preferences 存储在 Button 点击

Now, store preferences on Button click

enable = (Button) rootView.findViewById(R.id.enable);
enable.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

      editor.putInt("yourTheme", 2);
      editor.commit();
        listener.themechanged(2);
        enable.setVisibility(View.GONE);
        disable.setVisibility(View.VISIBLE);

    }
});
disable = (Button) rootView.findViewById(R.id.disable);
disable.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        editor.putInt("yourTheme", 1);
        editor.commit();
        listener.themechanged(1);
        disable.setVisibility(View.GONE);
        enable.setVisibility(View.VISIBLE);
    }
});

然后,在 MainActivityOnCreate() 中,您可以像这样检查

and then, In OnCreate() of MainActivity you can check like

SharedPreferences pref = getSharedPreferences("Theme", MODE_PRIVATE);
value= pref.getInt("yourTheme", 1);//1 is default value

if(value==2) {
    if (isDarkTheme) {
        appbackground.setVisibility(View.GONE);
        shade.setVisibility(View.GONE);
    } else {
        appbackground.setVisibility(View.VISIBLE);
        shade.setVisibility(View.VISIBLE);
    }
}else if(value==1){
        appbackground.setVisibility(View.GONE);
        shade.setVisibility(View.GONE);
}

完成,可能会有帮助

这篇关于如何在 SharedPreferences 中保存我的应用程序背景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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