Layout file.
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: background = "# ffffff"
Android: orientation = "vertical">
<TextView
Android: id = "@ + id/TV"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "mm I am crazy. -2"
Android: textColor = "@ color/text_color_selector"
Android: textSize = "25sp"/>
</RelativeLayout>
Resource file.
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<Selector xmlns: android = "http://schemas.android.com/apk/res/android">
<! -- Red -->
<Item android: state_selected = "true" android: color = "# ff0000"/>
<Item android: state_focused = "true" android: color = "# ff0000"/>
<Item android: state_pressed = "true" android: color = "# ff0000"/>
<! -- Brown -->
<Item android: color = "# 89683B"/>
</Selector>
Implementation code.
[Java]
Package com. sgw. textcolor;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. widget. TextView;
Public class TextColorDemoActivity extends Activity {
Private TextView mTextView;
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
MTextView = (TextView) findViewById (R. id. TV );
// Set the listener
MTextView. setOnClickListener (null );
}
}
When there is no click.
Click.
Note.
1. color resource files can be placed under res/color or res/drawable.
2. You must set an OnClick event for TextView to change the color.
Author: manoel