[英]Change the TEXT (not background) color of a spinner when an item is selected
I have a spinner with several options, each displaying a simple string. 我有一个带有几个选项的微调器,每个选项显示一个简单的字符串。 Initially, the text is all white.
最初,文本全是白色的。 However, if the user selects an option (causing it to become what is displayed on top), I would like that text to become red.
但是,如果用户选择一个选项(使其成为顶部显示的选项),我希望该文本变为红色。
How can I do this? 我怎样才能做到这一点?
EDIT : solved 编辑:解决了
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
((TextView) arg1).setTextColor(Color.parseColor("#E3170D"));
}
if the user selects an option (causing it to become what is displayed on top), I would like that text to become red.
如果用户选择一个选项(使其成为顶部显示的选项),我希望该文本变为红色。
So you most likely created OnItemSelectedListener() for your Spinner. 所以你很可能为你的Spinner创建了OnItemSelectedListener()。 So in onItemSelected() method you can simply change text color.
因此在onItemSelected()方法中,您只需更改文本颜色即可。
Pseudocode: 伪代码:
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
TextView selectedText = (TextView) parent.getChildAt(0);
if (selectedText != null) {
selectedText.setTextColor(Color.RED);
}
}
Hope it helps. 希望能帮助到你。
see this answer here and i will copy and paste it 在这里看到这个答案,我将复制并粘贴它
Selector: custom_selector.xml 选择器: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>
Custom View layout: my_simple_item 自定义视图布局: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);
Hope this helps 希望这可以帮助
some of you that using MaterialBetterSpinner
and Binding your Layouts, all the above won't help, try this, hope it helps you: 你们中的一些人使用
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 is something like this: 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>
create a spinner with this adapter and yourXML : 使用此适配器和yourXML创建一个微调器:
final MyAdapter adapter = new MyAdapter(getContext(), R.layout.yourXML, s.getValues());
final MaterialBetterSpinner spinner = new MaterialBetterSpinner(getContext());
spinner.setAdapter(adapter);
create like: 创建如:
<?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>
and in your activity xml: 并在您的活动xml中:
<Spinner...............
android:drawSelectorOnTop="true"
android:background="@drawable/sample"/>
Just add the OnItemSelectedListener
to your spinner. 只需将
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) {
}
});
use this to change the text of selected Text 使用它来更改所选文本的文本
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.