Android button Set text color colorstatelist

Source: Internet
Author: User

Windows platform VC, for different button states, the use of different color display text, the implementation of more complex, generally have to draw the button. But Android is easy to implement.

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

Java code
  1. <?xml version= "1.0" encoding= "Utf-8" ?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item android:state_pressed="true"
  4. android:color="#ffff0000"/> <!--pressed --
  5. <item android:state_focused="true"
  6. android:color="#ff0000ff"/> <!--focused --
  7. <item android:color="#ff000000"/> <!-- default ---
  8. </selector>
Java code
    1. Button btn= (button) Findviewbyid (R.ID.BTN);
    2. Resources resource= (Resources) Getbasecontext (). Getresources ();
    3. Colorstatelist csl= (colorstatelist) resource.getcolorstatelist (R.color.button_text);
    4. if (csl!=null) {
    5. Btn.settextcolor (color_state_list); //Set button text color   
    6. }

Or you can do this:

Java code
    1. Xmlresourceparser Xpp=resources.getsystem (). GETXML (R.color.button_text);
    2. Try  {  
    3. Colorstatelist csl= Colorstatelist.createfromxml (getresources (), xpp);
    4. Btn.settextcolor (CSL);
    5. } catch (Exception e) {
    6. //Todo:handle exception   
    7. }

Finally, attach all the possible statuses:

Java code
    1. <?xml version= "1.0" encoding= "Utf-8" ?>
    2. <selector xmlns:android="http://schemas.android.com/apk/res/android" >
    3. <item
    4. android:color="Hex_color"
    5. android:state_pressed=["true" | "false" ]  
    6. android:state_focused=["true" | "false" ]  
    7. android:state_selected=["true" | "false" ]  
    8. android:state_active=["true" | "false" ]  
    9. android:state_checkable=["true" | "false" ]  
    10. android:state_checked=["true" | "false" ]  
    11. android:state_enabled=["true" | "false" ]  
    12. android:state_window_focused=["true" | "false" ]/>
    13. </selector>

Transferred from: http://blog.csdn.net/ganlijianstyle/article/details/7593812

--------------------------------------------------------------------------------------------------------------- ---------------------------

Currentbutton.settextcolor (R.color.white);

The ID of this direct-value color goes in, and the text is found to be black, so you need to use SetTextColor (colorstatelist colors) method to pass in the Colorstatelist object

Colorstatelist whitecolor=getresources (). Getcolorstatelist (R.color.white);

Currentbutton.settextcolor (Whitecolor);

So the text can change color.

The Colorstatelist object can be defined in XML and used like a color, which can change color in real time based on the state of the View object it is applied to. For example, a button can exist in multiple states (pressed, focused, or other), and if you use Colorstatelist, you can provide different colors for each of its states.

Original: http://blog.csdn.net/meizhen51/article/details/6303612

Large-Scale Price Reduction
  • 59% Max. and 23% Avg.
  • Price Reduction for Core Products
  • Price Reduction in Multiple Regions
undefined. /
Connect with us on Discord
  • Secure, anonymous group chat without disturbance
  • Stay updated on campaigns, new products, and more
  • Support for all your questions
undefined. /
Free Tier
  • Start free from ECS to Big Data
  • Get Started in 3 Simple Steps
  • Try ECS t5 1C1G
undefined. /

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.