【问题标题】:Changing the textColor when the state changes in TextView in Android在Android中TextView中状态更改时更改textColor
【发布时间】:2013-07-15 07:44:06
【问题描述】:

嘿,当用户按下它时,我正在尝试更改TextView 中的textColor。我正在尝试在Windows 8 中制作类似超链接按钮的东西。我在res/color folder 中有这个选择器。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_hovered="true">
        <color android:color="@color/darkBlue"/>
    </item>
    <item  android:state_pressed="true">
        <color android:color="@color/lightBlue"/>
    </item>
    <item android:color="@color/black"/> <!-- default color -->
</selector>

我就是这样用的

<TextView
        android:id="@+id/tw_language"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/language_label"
        android:layout_marginRight="3dp"
        android:clickable="true"
        android:autoLink="all"
        android:text="@string/default_language_label"
        android:textColor="@color/language_button"

        />

在活动中,当我获得对此 textView 的引用时,我设置了 mLanguage.setPaintFlags(mLanguage.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);

获取带下划线的文本。并且应用程序使用它崩溃。如果我设置

android:background="@color/language_button"

而不是textColor 它工作正常。有谁知道我做错了什么?

【问题讨论】:

  • 只是补充一点,如果我 ser android:background 它会改变背景。我想改变 textColor。
  • 您想只更改文本颜色还是颜色和文本样式(在这种情况下为下划线)?
  • 你的问题还没有得到解答吗?

标签: android textview selector textcolor


【解决方案1】:

您需要向您的 TextView 添加属性,如下所示

<TextView
    android:id="@+id/txtResult"
    style="@drawable/language_button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
/>

style="@drawable/language_button" 是您的选择器文件。我已经在drawable/stack.xml 目录中定义了该文件。

【讨论】:

    【解决方案2】:

    你可以这样做:

    textView = (TextView)findViewById(R.id.myTextView);
        mMainView.setOnClickListener(new OnClickListener(){
    
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                textView.setTextColor(Color.GREEN);//set the color here
            }
    
        });
    

    【讨论】:

      猜你喜欢
      • 2012-04-19
      • 1970-01-01
      • 1970-01-01
      • 2013-08-10
      • 1970-01-01
      • 2016-07-15
      • 1970-01-01
      • 1970-01-01
      • 2011-05-01
      相关资源
      最近更新 更多