我有TextView的,我想改變顏色,當我專注或cliclked它像一個網頁鏈接文字我已經嘗試按照this但它仍然無法正常工作如何設置文字顏色爲鏈接?
請幫忙,謝謝
這是我的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"/>
什麼是您所面臨的問題? – Caner
它改變爲testcolor5,但它不能改變爲另一種顏色,我既不關注也不點擊 – user790156
張貼您的XML和代碼 – Caner