繁体   English   中英

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

[英]Change the TEXT (not background) color of a spinner when an item is selected

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

我怎样才能做到这一点?

编辑:解决了

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

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

所以你很可能为你的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. 创建自定义视图布局(例如,从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);

希望这可以帮助

你们中的一些人使用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);

创建如:

 <?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"/>  

只需将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) {

        }
    });

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM