簡體   English   中英

如何更改Android活動的背景顏色

[英]How to change background colour of an Android activity

我想使用AlertDialog更改活動的背景顏色。 這是我所做的:

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

我也將此代碼添加到OnCreate

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

選擇了顏色,並且Web視圖( wvContent )更改為選擇的顏色,但是不會將其保存到共享首選項和/或從“共享首選項”中加載。 重新啟動活動后,它將恢復為默認顏色。

所以問題是:當活動重新啟動時,如何正確地將所選顏色保存到SharedPreferences並加載它?

非常感謝您的幫助。

當前,您正在保存所選項目的位置,而不是在SharedPreferences中保存顏色代碼。 這樣做:

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()方法返回旋轉器中先前選擇的值的顏色代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM