Colorstatelist Change button, text box text color

Source: Internet
Author: User

The usual thing to do in a regular project is to change the background color of the button, the text verification, and the text box.

We know that it is easy and simple to use XML to make a selection. The XML code is as follows: This can be written directly in the 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=" #1c5e79 "/> <!--pressed--    <item android:state_focused= "true"          android:color= "#1c5e79"/> <!--focused--    <item Android:color= "#0b5979"/> <!--default--></selector>

Just add one line of code in the XML: android:textcolor= "@drawable/tv_traffic_textcolor_selector"

But how do you use this XML in dynamic code?

The main thing to say today is that if you implement this effect through code, the implementation can be done in two ways:

First Kind

Colorstatelist csl= (colorstatelist) getresources (). Getcolorstatelist (r.drawable.tv_traffic_textcolor_selector);  if (csl!=null) {  holder.tvTrafficTool.setTextColor (CSL);}

The second type:

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}

Are the same effect. ,


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Colorstatelist Change button, text box text color

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.