选择项目时,更改微调器的TEXT(非背景)颜色

时间:2013-03-22 07:12:31

标签: android

我有一个带有几个选项的微调器,每个选项都显示一个简单的字符串。最初,文本全是白色的。但是,如果用户选择一个选项(使其成为顶部显示的选项),我希望该文本变为红色。

我该怎么做?

编辑:已解决

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

6 个答案:

答案 0 :(得分:20)

  

如果用户选择一个选项(使其成为显示的选项)   在顶部),我希望该文本变为红色。

所以你最有可能为你的Spinner创建了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);
   }
}

希望它有所帮助。

答案 1 :(得分:2)

请参阅此回答here,我将复制并粘贴

  1. 创建自定义视图布局(例如,从TextView)
  2. 创建选择器并将其设置为该视图的背景
  3. 使用自定义视图设置微调器
  4. 选择器: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"/>
    

    Initialise Spinner:

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

    希望这有帮助

答案 2 :(得分: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);

答案 3 :(得分: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"/>  

答案 4 :(得分: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) {

        }
    });

答案 5 :(得分:0)

使用它来更改所选文本的文本

YOUR_SPINNER.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

     public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
         TextView selectedText=  view.findViewById(R.id.text_view_name_in_Adapter);
         selectedText.setTextColor(getResources().getColor(R.color.YOUR_COLOR));
        }
}