【问题标题】:How to set text color as link?如何将文本颜色设置为链接?
【发布时间】:2011-11-11 13:11:38
【问题描述】:

我有 textview,当我聚焦或点击它时,我想更改颜色

请帮忙,谢谢

这是我的java代码

public class TextColorActivity extends Activity {
/** Called when the activity is first created. */
 ColorStateList cl = null;
private TextView title;
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    title = (TextView)findViewById(R.id.hello); 
    try {
        Log.d("test","try");        
       XmlResourceParser xpp = getResources().getXml(R.drawable.selector_txt);
       cl = ColorStateList.createFromXml(getResources(), xpp);
    } catch (Exception e) {}
    title.setTextColor(cl);
    title.setFocusable(true);
    title.setClickable(true);
    title.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

                  Log.d("test","click");               

        }
    });
}

这是我的 selector_txt.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="@color/Darkgoldenrod"/>
<item android:state_pressed="true" android:state_enabled="false"  
android:color="@color/Darkgreen" />
<item android:state_enabled="false" android:color="@color/Red" />
<item android:color="@color/blue"/>

这是我的 main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
    android:id="@+id/hello" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello" 
    android:textSize="30dp"
    android:textStyle="bold"
    android:duplicateParentState="true"/>

【问题讨论】:

  • 您遇到了什么问题?
  • 它变成了 testcolor5 但它不能变成另一种颜色,我既不关注也不点击
  • 将此行更改为 ,如果它的工作与否更新我

标签: android textview


【解决方案1】:

如果需要,您还可以在 xml 中设置颜色。只需在您的 res 文件夹中创建一个颜色文件夹并将 xml 文件移动到那里,然后您可以通过android:textColor="@color/selector_txt"进行设置

关于您遇到的问题。 Android 将始终使用选择器中的第一个匹配项。如果按下 TextView,它也会被聚焦。因此,将android:pressed="false" 添加到您的第一项或将行移到按下状态行之后。

这是完整的xml:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:state_enabled="false" android:color="@color/Red" /> 
<item android:state_pressed="true" android:color="@color/Darkgreen" /> 
<item android:state_focused="true" android:color="@color/Darkgoldenrod"/>
<item android:color="@color/blue"/>
</selector>

【讨论】:

  • 我刚刚看到您还在按下状态下查找 enabled=false 。删除它并首先移动第三个项目然后它应该可以工作。我将用整个 xml 更新我的答案
  • 选择器应该可以正常工作。一定是其他地方有问题。你确定 onClick 事件被触发了吗?你能看到日志吗?
  • 您是否将选择器移至颜色文件夹?然后您可以通过 R.color.selector_txt 访问它,否则我没有想法
  • 是的,我已经把它搬走了,但它仍然不起作用,无论如何非常感谢你,如果你能找到其他想法,请帮助我:)
  • } catch (Exception e) {} 永远不要制作空的 catch 块。你确定那里没有任何异常吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-29
  • 2016-11-24
  • 1970-01-01
相关资源
最近更新 更多