繁体   English   中英

当状态在Android中的TextView中更改时更改textColor

[英]Changing the textColor when the state changes in TextView in Android

嘿,我试图在用户按下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"

        />

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

获得带下划线的文本。 使用此应用程序将崩溃。 如果我设置

android:background="@color/language_button"

代替textColor它可以正常工作。 有人知道我在做什么错吗?

您需要将属性添加到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目录中定义了该文件。

你可以这样做:

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
        }

    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM