How to set the color of a different set of States for a button

Source: Internet
Author: User

  Previously encountered a problem in the development process: How to display a button or a View object when clicked or not clickable or pressed to show a different state, and later learned that there is a resource type: <SELECTOR> Can be used to define a different color or pattern based on the state of the control.
For example, we can define a button that appears dimmed when it is disabled, green when it is turned on, and yellow when pressed. When we have the selector element we can define a number of Itme sub-elements below it to specify our properties, I can look at some of the properties of the following table:

Color Status list <item> element properties
Property Explain
Color Specifies a hexadecimal color for one of the following types must attribute: #RGB, #ARGB, #RRGGBB, #ARRGGBB.
State_enabled A Boolean type that determines whether the object can receive touch or click events with the value: TRUE or FALSE.
State_checked A Boolean type that determines whether the object is selected with a value of: TRUE or false.
State_checkable A Boolean type that determines whether the object can be selected with a value of: TRUE or false.
state_selected A Boolean type that determines whether the object is selected with a value of: TRUE or false.
State_focused A Boolean type that determines whether the object gets the focus and the value: TRUE or FALSE.
State_pressed A Boolean type that determines whether the object is pressed with a value of: TRUE or false.

  

  So how do you use it? Next, let's take an example:

We create a new XML file under Res/color, named Textcolor.xml, for the following:

1 <selectorxmlns:android= "Http://schemas.android.com/apk/res/android">2     3     <Item4         android:state_enabled= "true"5 Android:color= "#FF00FF"6         />7     8     <Item9         android:state_pressed= "true"Ten Android:color= "00FF00" One         /> A     <Item -         android:state_focused= "true" - Android:color= "#FF0000" the         /> -     <Item -         Android:color= "#000000" -         /> +      - </selector>

  Then we define a Button,button in Main_activity.xml that can be written like this:

< Button          Android:layout_height = "Wrap_content"         android:layout_width= "Wrap_content"        android:text= "HelloWorld"         android:textcolor= "@color/textcolor"        />

  This allows the button to be applied to the properties we need. In fact, it is quite simple, but when we first did not learn selector in the Java file to write a lot of useless code, this is also a small amount of code to reduce the number of tricks.

BOB

Continuous update ...

How to set the color of a different set of States for a button

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.