Android RadioButton textColor 选择器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21945134/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:20:51  来源:igfitidea点击:

Android RadioButton textColor selector

androidradio-button

提问by Axarydax

I have a selector for textColorof a RadioButtonlike this:

我有一个选择textColorRadioButton是这样的:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:color="#fff"/>
    <item android:state_focused="true" android:color="#f00"/>
    <item android:state_pressed="true" android:color="#0f0"/>
    <item android:state_focused="false" android:state_pressed="false" android:color="#00f"/>
</selector>

I expected that the one selected RadioButtonwill have different color than the others.

我预计所选的RadioButton颜色将与其他颜色不同。

However, all of the RadioButtons have blue text (using android:state_focused="false" android:state_pressed="false"), even the one that is selected.

然而,所有的RadioButtons 都有蓝色文本(使用 android:state_focused="false" android:state_pressed="false"),即使是被选中的那个。

What am I doing wrong?

我究竟做错了什么?

回答by Grant Amos

It looks like you're just using the wrong selectors. The docs describe selecting as follows:

看起来您只是使用了错误的选择器。文档描述了如下选择:

During each state change, the state list is traversed top to bottom and the first item that matches the current state is used—the selection is not based on the "best match," but simply the first item that meets the minimum criteria of the state.

在每次状态更改期间,从上到下遍历状态列表并使用与当前状态匹配的第一个项目——选择不是基于“最佳匹配”,而是基于满足状态最低标准的第一个项目.

Source link

源码链接

So, in order:

所以,按顺序:

  1. state_selected is never true as RadioButtons use state_checked when checked.
  2. state_focused is never called because RadioButton will never receive input focus.
  3. state_pressed should be working. When you hold your finger down you don't see the text appearing green?
  4. state_focused false and state_pressed false ends up being default so you see blue.
  1. state_selected 永远不会为真,因为 RadioButtons 在选中时使用 state_checked。
  2. state_focused 永远不会被调用,因为 RadioButton 永远不会接收输入焦点。
  3. state_pressed 应该可以工作。当您按住手指时,您没有看到文本显示为绿色?
  4. state_focused false 和 state_pressed false 最终成为默认值,因此您会看到蓝色。

If you would like to see different states, try these:

如果您想查看不同的状态,请尝试以下操作:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:color="#0f0"/>
    <item android:state_checked="true" android:color="#fff"/>
    <item android:color="#00f"/>
</selector>

I have tested the above and can see all colors being expressed appropriately.

我已经测试了上述内容,可以看到所有颜色都得到了适当的表达。

回答by Isaias Carrera

According to Android. https://developer.android.com/guide/topics/resources/color-list-resource.html. https://developer.android.com/reference/android/content/res/ColorStateList.html

根据安卓。 https://developer.android.com/guide/topics/resources/color-list-resource.htmlhttps://developer.android.com/reference/android/content/res/ColorStateList.html

You have to create a folder called 'color' in 'res' directory and create a new file called radiobuttonstate.xml for example which it looks like this.

您必须在“res”目录中创建一个名为“color”的文件夹,并创建一个名为 radiobuttonstate.xml 的新文件,例如它看起来像这样。

 <?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:state_focused="true"
    android:color="YOUR COLOR" />

   <item
    android:state_pressed="true"
    android:state_enabled="false"
    android:color="YOUR COLOR" />

    <item android:color="YOUR COLOR"
    android:state_checked="true"/>

    <item
    android:state_enabled="false"
    android:color="YOUR COLOR" />

   <item android:color="YOUR COLOR" />
</selector>

then in your radio button define in the android:textColor attribute your color list you previously defined.

然后在您的单选按钮中的 android:textColor 属性中定义您之前定义的颜色列表。

  <RadioButton
                    android:id="@+id/radio_H"
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:text="@string/string_example"
                    android:textColor="@color/radiobuttonstate"
                    android:textAlignment="center" />

回答by Rohan Kandwal

The answer provided by @GrantAmos is perfect and working. If you want to text color selector through XML, please use this code.

@GrantAmos 提供的答案是完美且有效的。如果您想通过 XML 文本颜色选择器,请使用此代码。

android:textColor="@color/textview_selector"

android:textColor="@color/textview_selector"

However, if you want to set the selector programmatically, use this code -

但是,如果要以编程方式设置选择器,请使用此代码 -

radioButton.setTextColor(ContextCompat.getColorStateList(getContext(), R.color.textview_selector));

Hope it will save someone's time.

希望它会节省某人的时间。

回答by Ramesh_D

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

This one works for me. Actually when i use android:color="@color/dark_grey". It didn't work. But when i changed to drawable it did.

这个对我有用。实际上,当我使用 android:color="@color/dark_grey" 时。它没有用。但是当我改为 drawable 时它确实如此。