Android Technology Summary (006) -- dynamically changing the color of TextView

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.