2011-09-29 63 views
2

我需要爲顏色列表創建微調。我將選取所選項目,獲取所選顏色並將此顏色設置爲其他元素。我想在.xml中設置顏色列表,因爲我有幾個spinners,並且想爲它創建資源。但是,如果我創建一個簡單的密鑰對列表,在代碼中我必須創建許多塊(如果有的話)來檢查顏色。我如何創建和使用資源文件(對「字符串INT」)爲微調?謝謝如何在Java,Android上創建顏色列表?

+2

你可以嘗試改變你的問題嗎?它真的不清楚。我並沒有明白你的意思,'如果有其他許多塊'來檢查collors加上你讓人們從微調選擇的事實。 – Peter

+0

我可以從類似spinner.getSelectedItem.toString()獲得類似spinner.getSelectedItem.toString的值,但爲了檢查顏色,我必須使用if-else:if(value.equals(「Red」))和許多類似的塊。 – user963313

回答

6

您已經知道如何在Spinner中顯示數據。

String Array顯示Spinner中的數據。

考慮String[] array={"Green","Blue","Red"};

立即採取一個其它陣列的顏色,使得它的顏色相匹配的第一陣列中..

這裏有2個選項即串或int數組

字符串數組=>String[] arrayColors={"#00ff00","#0000ff","#ff0000"};

int數組=>int [] arrayColors={Color.GREEN,Color.BLUE,Color.Red}

使用任何一種。 (推薦:使用int數組,因爲在使用時不必分析顏色)

因此,您建立了兩個數組之間的一對一對應關係。

立即註冊OnItemSelectedListener聽衆收聽選擇在Spinner

yourSpinner.setOnItemSelectecListener(new OnItemSelectedListener() { 
    public void onItemSelected(AdapterView<?> parent, View view, 
      int pos, long id) { 
     // Change color of other views by using pos argument 

     // IF YOU HAVE USED String Array 
     yourView.setBackgroundColor(Color.parseColor(arrayColors[pos])); 

     // IF YOU HAVE USED int Array 
     yourView.setBackgroundColor(arrayColors[pos]); 
    } 

    public void onNothingSelected(AdapterView parent) { 
     // Do nothing. 
    } 
}); 
1

沒有像這樣的幫助?

String[] colorList = {"white", "black"}; 
int[] color = {Color.WHITE, Color.BLACK}; 
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, colorList); 

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
modeSpinner.setAdapter(adapter); 

modeSpinner.setOnItemSelectedListener(new OnItemSelectedListener() 
{ 
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) 
    { 
     yourView.setColor(color.get(colorList.getSelectedItemPosition())     
    } 
    public void onNothingSelected(AdapterView<?> arg0) 
    { 
    //... 
    } 
});