[英]How to set text color as link?
I have textview which I want to change the color when I focus or cliclked it like a link text in web I have try to follow this but it still doesn't work 我有textview,当我聚焦或像链接文本一样倾斜颜色时,我想改变颜色,我尝试按照此方法进行操作,但仍然无法正常工作
please help, thanks 请帮助,谢谢
this is my java code 这是我的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");
}
});
}
this is my selector_txt.xml 这是我的选择器_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"/>
and this is my main.xml 这是我的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"/>
You can also set your color in the xml if you want. 如果需要,还可以在xml中设置颜色。 Just create a color folder in your res folder and move the xml file there then you can set it via
android:textColor="@color/selector_txt"
只需在res文件夹中创建一个颜色文件夹并将xml文件移动到那里,然后您就可以通过
android:textColor="@color/selector_txt"
Regards the problem you're having. 关于您遇到的问题。 Android will always use the first match in a selector.
Android将始终使用选择器中的第一个匹配项。 If a TextView is pressed it is also focused.
如果按下TextView,它也会被聚焦。 So add
android:pressed="false"
to your first item or move the line after the pressed status line. 因此,将
android:pressed="false"
到您的第一个项目中,或者将其移动到按下状态行之后。
That's the full xml: 这是完整的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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.