2013-03-22 155 views
11

我有一個微調器,有幾個選項,每個選項都顯示一個簡單的字符串。最初,文本全白。但是,如果用戶選擇一個選項(使其成爲頂部顯示的內容),我希望該文本變爲紅色。選擇某個項目時更改微調器的TEXT(不是背景)顏色

我該怎麼做?

編輯:解決

public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { 
    ((TextView) arg1).setTextColor(Color.parseColor("#E3170D")); 
} 
+0

提到這個http://stackoverflow.com/questions/7584158/setting-background-color-for-spinner-item-on-selection – rajeshwaran 2013-03-22 07:14:16

回答

18

如果用戶選擇一個選項(使其成爲什麼顯示在頂部 ),我想文本變紅。

所以你很可能爲你的微調創建OnItemSelectedListener()。因此,在onItemSelected()方法中,您可以簡單地更改文本顏色。

僞代碼:

public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { 
    TextView selectedText = (TextView) parent.getChildAt(0); 
    if (selectedText != null) { 
     selectedText.setTextColor(Color.RED); 
    } 
} 

希望它能幫助。

+0

這絕對有效,我實際上已經算了幾分鐘前,但尚未公佈它。一個小修改雖然,看到我的編輯,如果你很好奇 – drewmoore 2013-03-22 09:02:34

+1

這不工作正確,當你旋轉它雖然。在這裏看到我的問題:http://stackoverflow.com/questions/33747884/android-nullpointerexception-spinner-onitemselected-view-parameter-is-null-a – 2015-11-17 04:45:58

2

看到這個答案here,我將其複製並粘貼

  1. 創建自定義視圖佈局(例如,從TextView的)
  2. 創建選擇和設置作爲該視圖的背景
  3. 設置自定義視圖的微調器

選擇:custom_selector.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_focused="true" 
      android:state_pressed="false" 
      android:drawable="@color/light_grey" /> 
    <item android:state_focused="true" 
      android:state_pressed="true" 
      android:drawable="@color/light_grey" /> 
    <item android:state_focused="false" 
      android:state_pressed="true" 
     android:drawable="@color/light_grey" /> 
    <item android:state_selected="true" android:drawable="@color/light_grey"/> 
    <item android:drawable="@color/white" /> 
</selector> 

自定義視圖佈局:my_simple_item

<?xml version="1.0" encoding="utf-8"?> 
<TextView 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:lines="1" 
android:padding="5dip" 
android:background="@drawable/custom_selector"/> 

初始化微調:

String[] items = new String[] {"One", "Two", "Three"}; 
Spinner spinner = (Spinner) findViewById(R.id.mySpinner); 
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.my_simple_item, items); 

希望這有助於

+0

我有點困惑。我創建了「選擇器」(上面的項目),在樣式xml中,對不對?那麼我在哪裏將這種風格應用到my_simple_item? – drewmoore 2013-03-22 07:19:36

+0

我認爲這會改變文本視圖的背景,但不是文本顏色 – 2013-03-22 07:20:22

+0

在你的微調,只是使用這一行'android:background =「@ drwable/custom_selector」' – 2013-03-22 07:21:46

0

建立這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:state_pressed="false" android:drawable="@color/red" /> 
<item android:drawable="@android:color/transparent" /> 
    </selector> 

,並在活動的xml:

<Spinner............... 
android:drawSelectorOnTop="true" 
    android:background="@drawable/sample"/> 
0

只需將OnItemSelectedListener添加到您的微調器。

qtySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
     @Override 
     public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
      ((TextView) view).setTextColor(Color.BLACK); //Change selected text color 
     } 

     @Override 
     public void onNothingSelected(AdapterView<?> parent) { 

     } 
    }); 
2

一些你在使用MaterialBetterSpinner和綁定你的佈局, 上述所有無助,試試這個,希望它可以幫助你:

public class MyAdapter extends ArrayAdapter<String> {  

    public MyAdapter(Context context, int textViewResourceId, List<String> objects) { 
     super(context, textViewResourceId, objects);   

    } 

    @Override 
    public View getDropDownView(int position, View convertView, ViewGroup parent) { 
     return getCustomView(position, convertView, parent); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     return getCustomView(position, convertView, parent); 
    } 

    public View getCustomView(int position, View convertView, ViewGroup parent) { 
     LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     final YourXMLBinding rowBinding = DataBindingUtil.inflate(inflater, R.layout.yourXML, parent,false); 
     rowBinding.tv1.setText(mMy.getValues().get(position)); 
     if(position == mMy.getCurrentIndex()) { 
      rowBinding.tv1.setTypeface(Typer.set(getContext()).getFont(Font.ROBOTO_BOLD));//change font 
      rowBinding.tv1.setTextColor(ContextCompat.getColor(getContext(), R.color.yourColor));//change color 
     } 
     return rowBinding.getRoot(); 
    } 
} 

yourXML是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<layout> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:background="@color/colorBackgroundStart"> 
    <TextView 
     android:id="@+id/tv1" 
     android:layout_width="0dp" 
     android:layout_weight="0.7" 
     android:layout_height="30dp" 
     android:textColor="#fff" 
     android:textSize="16dp" 
     android:layout_marginTop="8dp" 
     android:layout_marginBottom="10dp" 
     android:layout_marginLeft="8dp"/> 

</layout> 

創建與此適配器和yourXML一個微調:

final MyAdapter adapter = new MyAdapter(getContext(), R.layout.yourXML, s.getValues()); 

final MaterialBetterSpinner spinner = new MaterialBetterSpinner(getContext()); 
spinner.setAdapter(adapter);