【问题标题】:Change the TEXT (not background) color of a spinner when an item is selected选择项目时更改微调器的文本(非背景)颜色
【发布时间】:2019-03-01 19:57:02
【问题描述】:

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

我该怎么做?

编辑:已解决

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

【问题讨论】:

标签: android


【解决方案1】:

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

所以您很可能为您的 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);
   }
}

希望对你有帮助。

【讨论】:

  • 这绝对有效,我实际上在几个静音前就想出来了,但还没有发布。一个小的修改,如果你好奇,请看我的编辑
  • 当你旋转它时这不起作用。在这里查看我的问题:stackoverflow.com/questions/33747884/…
【解决方案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);

希望对你有帮助

【讨论】:

  • 我有点困惑。我在样式 xml 中创建了“选择器”(顶部的项目),对吗?那么我在哪里将该样式应用于 my_simple_item?
  • 我认为这会改变 textview 的背景,但不会改变 textcolor
  • 在您的微调器上,只需使用此行android:background="@drwable/custom_selector"
  • 你的 custom_selector 也必须在 drawable 文件夹中
  • @WilliamKinaan:我调整了这段代码(android:textcolor 而不是android:background),它可以在微调器关闭时改变文本的颜色,但我想要的是改变选择一项时顶部项目的颜色。因此上面发布了答案..感谢您的帮助,+1
【解决方案3】:

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

【讨论】:

    【解决方案4】:

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

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

    【讨论】:

      【解决方案5】:

      创建喜欢:

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

      【讨论】:

        【解决方案6】:

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

        【讨论】:

          猜你喜欢
          • 2018-04-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-02-01
          • 2013-03-01
          • 1970-01-01
          相关资源
          最近更新 更多