Android programming to realize different colors of different state text for the control _android

Source: Internet
Author: User

This article describes the Android programming implementation of the control of different state text display different colors of the method. Share to everyone for your reference, specific as follows:

Mode one :

The first control to select

<textview
   android:layout_width= "wrap_content"
   android:layout_height= "Wrap_content"
   @+id/close_time_display "
   android:layout_marginright=" 20DP "
   android:text=" @string/default_time "
   style= "@style/item_content_text_style"/>

Style is a custom style, and the corresponding XML file is as follows:

<style name= "Item_content_text_style" >
    <item name= "android:textsize" >26sp</item>
    <item name= "Android:duplicateparentstate" >true</item>
    <item name= "Android:textcolor" >@ Drawable/textcolor_yellow_selector</item>
</style>

The Textcolor_yellow_selector in TextColor is as follows

<?xml version= "1.0" encoding= "Utf-8"?> <selector xmlns:android=
 "http://schemas.android.com/apk/" Res/android "
 >
 <item
 android:state_pressed=" true "
 android:color=" @color/yellow "/>
 <item
 android:state_focused= "true"
 android:color= "@color/yellow"/>
 <item android: State_selected= "true"
 android:color= "@color/yellow" ></item>
 <item android:color= "@color/ White "/>
</selector>

Implementation mode two : colorstatelist text color

Api

Windows platform VC, for different button states, using a different color display text, to achieve more complex, generally have to draw a button. But Android is easy to implement.

We first add a colorstatelist resource XML file, which is saved in Res/color/button_text.xml:

<?xml version= "1.0" encoding= "Utf-8"?> <selector xmlns:android=
"http://schemas.android.com/apk/res/" Android ">
  <item android:state_pressed=" true "
     android:color=" #ffff0000 "/> <!--pressed-->
  <item android:state_focused= "true"
     android:color= "#ff0000ff"/> <!--focused-->
  <item Android:color= "#ff000000"/> <!--default-->
</selector>

Button btn= (button) Findviewbyid (R.ID.BTN);
Resource= (resources) Getbasecontext () getresources ();
Colorstatelist csl= (colorstatelist) resource.getcolorstatelist (r.color.button_text);
if (csl!=null) {
   btn.settextcolor (color_state_list);//Set button text color
}

Or you can do this:

Xmlresourceparser Xpp=resources.getsystem (). GETXML (R.color.button_text);
try {
   colorstatelist csl= colorstatelist.createfromxml (getresources (), xpp);
   Btn.settextcolor (CSL);
} catch (Exception e) {
   //Todo:handle Exception
}

Finally attach any states that may appear:

<?xml version= "1.0" encoding= "Utf-8"?> <selector xmlns:android=
"http://schemas.android.com/apk/res/" Android >
<item
  android:color= "Hex_color"
  android:state_pressed=["true" | "false"]
  android:state_focused=["true" | "false"]
  android:state_selected=["true" | "false"]
  android:state_active=["true" | "false"]
  android:state_checkable=["true" | "false"]
  android:state_checked=["true" | "false"]
  android:state_enabled=["true" | "false"]
  android:state_window_focused=["true" | "false"]/>
</selector>

For more information on Android-related content readers can view the site topics: "Android Development Introduction and Advanced Course", "Android Multimedia operating skills Summary (audio, video, recording, etc.)", "Android Basic Components Usage Summary", " Android View tips Summary, Android layout layout tips and a summary of Android controls usage

I hope this article will help you with the Android program.

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.