首页 文章

Android Studio:尝试根据共享Pref更改背景,但在强制关闭时重置为默认背景

提问于
浏览
1

所以,我尝试基于共享偏好onCreate更改背景,如下所示:

**strong text**
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        prefManager = new PrefManager(this);
        String imagename = prefManager.getBg();
        if (imagename=="1") {
            RelativeLayout settinglin = (RelativeLayout) findViewById(R.id.settinglin);
            settinglin.setBackgroundResource(R.drawable.lginbg);
        } else if(imagename=="2") {
            RelativeLayout settinglin = (RelativeLayout) findViewById(R.id.settinglin);
            settinglin.setBackgroundResource(R.drawable.lginbgload);
etc...

它适用于此功能,但问题是,当应用程序强制关闭时,它总是将背景图片返回到默认背景(在布局中),例如:

android:background="@drawable/lginbg"

我检查强制关闭时我的共享首选项的值是否未重置(因此问题不在共享首选项中)

有人可以帮我解决这个问题

1 回答

  • 0

    == 测试参考相等性(它们是否是同一个对象) .

    .equals() 测试 Value 平等(无论它们是否逻辑"equal") .

    你应该使用equals代替 .

    if (imagename.equals("1"))
    

相关问题