使用共享首选项更改背景

iyzzxitl  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(373)

我有一个应用程序,允许我的用户自定义我的应用程序使用按钮的背景。我的应用程序是这样工作的:首先它会引导他们进入我的主要活动,在那里有一个按钮,他们可以按下自定义背景。当他们按下这个按钮时,它将引导他们进入一个对话框片段,用户可以选择他们想要的背景图像。我可以改变我的背景,但是,共享的偏好没有正常工作。当我关闭并打开我的应用程序时,它会变回默认背景,但当我按下对话框片段按钮时,它会将背景更新为他们选择的任何内容。
所以基本上,只有当我打开提供背景图像的按钮时,背景才会更新。
我不确定我是否解释得很好,所以这里是我的问题的gif
背景只有在我按下地形按钮时才会更新,有人有办法解决这个问题吗?我对android和java还很陌生,所以我不确定我是不是遗漏了什么。。。
流行主题.java

public class PopupTheme extends DialogFragment implements View.OnClickListener {

    private ImageButton btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn10;

    private static final String BG_NAME = "bgName";
    private static final String BG_KEY = "bg";

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view =  inflater.inflate(R.layout.activity_popup_theme, container, false);

        btn1 =  view.findViewById(R.id.btn1);
        btn2 = view.findViewById(R.id.btn2);

        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);

        if (getBackground() != R.drawable.bgscreen1 ){
             MainActivity.mainLayout.setBackgroundResource(getBackground());
        }

        return view;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn1:
                MainActivity.mainLayout.setBackgroundResource(R.drawable.bgscreen1);
                Toast.makeText(getContext(), "Clicked", Toast.LENGTH_SHORT).show();

                storeBackground(R.drawable.bgscreen1);
                break;

            case R.id.btn2:
                MainActivity.mainLayout.setBackgroundResource(R.drawable.bgscreen2);
                Toast.makeText(getContext(), "Clicked", Toast.LENGTH_SHORT).show();

                storeBackground(R.drawable.bgscreen2);
                break;
        }
    }

    public void storeBackground(int background) {
        SharedPreferences sharedPreferences = getActivity().getSharedPreferences(BG_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();     //accessing the shared pref
        editor.putInt(BG_KEY, background);
        editor.apply(); 
    }

    //    getting the background
    public  int getBackground() {

        SharedPreferences sharedPreferences = getActivity().getSharedPreferences(BG_NAME, Context.MODE_PRIVATE);
        int selectedBG = sharedPreferences.getInt(BG_KEY, R.drawable.bgscreen1); 
        return selectedBG;
    }

}

主活动.java

public class MainActivity extends AppCompatActivity {

    private Button btnWatch, btnReadStory, btnFavorites, btnAbout, btnListen;
    private ImageButton btnTheme;

    static ConstraintLayout mainLayout;

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

        btnTheme = findViewById(R.id.btnTheme);
        mainLayout = findViewById(R.id.layoutMain);

        btnTheme.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PopupTheme popupTheme = new PopupTheme();
                popupTheme.show(getSupportFragmentManager(), "Popup Theme");

            }
        });
    }

}
xqkwcwgp

xqkwcwgp1#

问题是,只有在打开poputheme对话框时才更新mainactivity的背景,因此需要将getbackground和更新代码从poputheme移到mainactivity上的oncreate方法,以便在用户启动应用程序时执行代码

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

     btnTheme = findViewById(R.id.btnTheme);
     mainLayout = findViewById(R.id.layoutMain);

     int background = getBackground();
     if (background  != R.drawable.bgscreen1 ){
        mainLayout.setBackgroundResource(background);
     }

     // Other code on onCreate
}

// Move getBackground method here from PopupTheme

注意:您可以使用-1作为背景的默认值,并检查是否为-1表示用户使用默认背景

相关问题