如何改变一个Android活动的背景颜色 [英] How to change background colour of an Android activity

查看:186
本文介绍了如何改变一个Android活动的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想改变使用AlertDialog活动的背景色。这是我做了什么:

I want to change the background colour of an activity using AlertDialog. Here is what I have done:

private SharedPreferences prefs;    
private static final String SELECTED_ITEM = "SelectedItem"; 
private Editor sharedPrefEditor;

btnchangeColor = (ImageButton) findViewById(R.id.btnchangeColor);
btnchangeColor.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {

        final CharSequence[] items={"Red","Green","Blue", "Yellow", "#EE82EE"};

        AlertDialog.Builder builder = new AlertDialog.Builder(
                ContentView_2.this);

        builder.setTitle("Pick a Color");
        builder.setPositiveButton("ok", new DialogInterface.OnClickListener() { 

            @Override
            public void onClick(DialogInterface dialog, int which) {}
        });

        builder.setSingleChoiceItems(items, getSelectedItem(), new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                wvContent = (WebView) findViewById(R.id.wvContent);
                //LinearLayout ll=(LinearLayout)findViewById(R.id.wvContent); 

                if("Red".equals(items[which]))
                {
                    wvContent.setBackgroundColor(Color.RED);
                }
                else if("Green".equals(items[which]))
                {
                    wvContent.setBackgroundColor(Color.GREEN);
                    }
                else if("Blue".equals(items[which]))
                {
                    wvContent.setBackgroundColor(Color.BLUE);
                    }
                else if("Yellow".equals(items[which]))
                {
                    wvContent.setBackgroundColor(Color.YELLOW);
                    }
                else if("#EE82EE".equals(items[which]))
                {
                    wvContent.setBackgroundColor(Color.rgb(184,184,184));
                    }
                saveSelectedItem(which);
            }
        });
        builder.show();

    }});
    }

private int getSelectedItem() {
    if (prefs == null) {
        prefs = PreferenceManager
                .getDefaultSharedPreferences(this);
    }
    return prefs.getInt(SELECTED_ITEM, -1);
}

private void saveSelectedItem(int which) {
    if (prefs == null) {
        prefs = PreferenceManager
                .getDefaultSharedPreferences(this);
    }
    sharedPrefEditor = prefs.edit();
    sharedPrefEditor.putInt(SELECTED_ITEM, which);
    sharedPrefEditor.commit();
}

我还添加此code到的OnCreate

wvContent = (WebView) findViewById(R.id.wvContent); 
    wvContent.setBackgroundColor(getSelectedItem());

选择的颜色和Web视图( wvContent )的变化来选择颜色,但这不是保存和/或共享preferences加载。它返回到活动时重新启动其默认的颜色。

The colour is selected and the web view (wvContent) changes to chosen colour, BUT this is not saved to and/or loaded from Shared Preferences. It returns to its default colour when the activity is relaunched.

所以,问题是:如何才能正确地我保存选定的颜色来共享preferences并加载活动时重新登场

So the question is: How can properly I save the selected colour to SharedPreferences and load it when activity relaunches?

非常感谢您的帮助。

推荐答案

目前要保存在共享preferences选择的项目,而不是颜色code的位置。做到这一点的:

Currently you are saving position of selected item instead of color code in SharedPreferences. do it as:

wvContent = (WebView) findViewById(R.id.wvContent);
int bg_color=Color.TRANSPARENT;
 if("Red".equals(items[which]))
   {
       wvContent.setBackgroundColor(Color.RED);
       bg_color=Color.RED;
   }
 else if("Green".equals(items[which]))
  {
      wvContent.setBackgroundColor(Color.GREEN);
       bg_color=Color.GREEN;
  }
........
// save color in SharedPreferences
 saveSelectedItem(bg_color);

现在 getSelectedItem()方法返回的颜色code为previously选择的值由微调。

Now getSelectedItem() method return color code for previously selected value from spinner.

这篇关于如何改变一个Android活动的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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