[英]Changing the textColor when the state changes in TextView in Android
Hey I am trying to change the textColor
in TextView
when the user press it. 嘿,我试图在用户按下
TextView
时更改textColor
。 I am trying to make something like hyperlink button in Windows 8
. 我正在尝试使类似
Windows 8
超链接按钮。 I have this selector in res/color folder
. 我在
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>
and I use it like this 我这样用
<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"
/>
In activity when I get the reference to this textView I set mLanguage.setPaintFlags(mLanguage.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
在活动中,当我获得对此
mLanguage.setPaintFlags(mLanguage.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
的引用时,我设置了mLanguage.setPaintFlags(mLanguage.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
to get underlined text. 获得带下划线的文本。 And the app crashes using this.
使用此应用程序将崩溃。 If I set
如果我设置
android:background="@color/language_button"
Instead of textColor
it works fine. 代替
textColor
它可以正常工作。 Does anyone know what I am doing wrong? 有人知道我在做什么错吗?
You need to add attribute to your TextView like below 您需要将属性添加到TextView中,如下所示
<TextView
android:id="@+id/txtResult"
style="@drawable/language_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
style="@drawable/language_button"
is your selector file. style="@drawable/language_button"
是您的选择器文件。 I have defined that file in drawable/stack.xml
directory. 我已经在
drawable/stack.xml
目录中定义了该文件。
You can do this: 你可以这样做:
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.