Android-selector usage

Source: Internet
Author: User

In the development of the project, because the system gives the control is not beautiful, so the development of the leadership often want me to change the next interface, using art to replace the image of the system. At first, I just set a background image for the button and so on, although it is beautiful, but the interface looks rather rigid, such as the user clicked the button, the button did not react. I then add the control to the Ontouch listen to the event, press the background to change the color, let go and then restore the original color. But later found selector this weapon, is really overjoyed, no longer add Ontouch monitoring events, use up also convenient and flexible. I have to say that it is necessary to communicate technical experience with other developers, especially if I am solely responsible for the development of an app like myself.

Android selector to be configured under Drawable.
The properties that selector can set are:
android:state_pressed If it is true, displays the picture when it is clicked, and if False is not pressed, the default is displayed.

android:state_focused            If True, gets the focus when the If False does not get the focus display default.

android:state_selected           If True, when selected Displays the picture if it is false and is not selected.

android:state_checkable          If the value is true when the checkbox can Displays the picture when it is used; false to display the picture when the checkbox is not available.

android:state_checked           If the value is true when CHEC Kbox the picture when selected, False to display the checkbox when it is selected.

android:state_enabled            If the value is true, The picture is displayed when the component is available, false, and displayed when the component is not available.
 
android:state_window_focused     If the value is true, the picture is displayed when the activity obtains the first focus; Fals E, the picture is displayed when it is not in the front

These property values can also be superimposed, such as:
Android:state_window_focused= "true" android:state_pressed= "true"

Indicates the background picture when the focus is acquired in non-touch mode and clicked.

In general, the button control only needs to be android:state_pressed

Example:

 1 <?xml version= "1.0" encoding= "Utf-8"?> 2 <linearlayout xmlns:android= "http://schemas.android.com/apk /res/android "3 android:layout_width=" match_parent "4 android:layout_height=" match_parent "5 Android:orientati on= "Horizontal" > 6 7 <radiogroup 8 android:id= "@+id/radiogroup1" 9 android:layout_width= "match _parent "Ten android:layout_height=" 60DP "one android:orientation=" horizontal "android:background=" @drawable/group_buton_nomal "android:gravity=" center ">14 <radiobutton16 android:id=             "@+id/radio0" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" 19                Android:checked= "true" android:text= "@string/weixin" style= "@style/radiostyle" 22  android:drawabletop= "@drawable/tab_weixin"/>23 </radiogroup>25 </linearlayout> 

Then we create a tab_weixin.xml android XML file, type Select Selector

1 <?xml version= "1.0" encoding= "Utf-8"? >2 <selector xmlns:android= "http://schemas.android.com/apk/res/ Android ">3     <item android:state_checked=" true "4         android:drawable=" @drawable/tabbar_mainframehl " ></item>5     <item 6         android:drawable= "@drawable/tabbar_mainframe" ></item>7 8 </ Selector>

The above is a simple selector use, that is, the button in the loss of focus on the operation, the advantages of Java-like If...else

<?xml version= "1.0" encoding= "Utf-8"? ><selector xmlns:android= "http://schemas.android.com/apk/res/ Android ">     <item android:state_pressed=" true "android:drawable=" @drawable/bg_selected ">        </ item>    <item android:drawable= "@drawable/bg_unselect" >            </item></selector>

At work, because the system gives the control is not beautiful, so the development of the leadership often want me to change the next interface, with art-given pictures instead of system pictures. At first, I just set a background image for the button and so on, although it is beautiful, but the interface looks rather rigid, such as the user clicked the button, the button did not react. I then add the control to the Ontouch listen to the event, press the background to change the color, let go and then restore the original color. But later found selector this weapon, is really overjoyed, no longer add Ontouch monitoring events, use up also convenient and flexible. I have to say that it is necessary to communicate technical experience with other developers, especially if I am solely responsible for the development of an app like myself.
      
      android selector to be configured under Drawable.
      The properties that selector can set are:
  android:state_pressed    ;         If it is true, displays the picture when it is clicked, and if False is not pressed, the default is displayed.

android:state_focused            If True, gets the focus when the If False does not get the focus display default.

android:state_selected           If True, when selected Displays the picture if it is false and is not selected.

android:state_checkable          If the value is true when the checkbox can Displays the picture when it is used; false to display the picture when the checkbox is not available.

android:state_checked           If the value is true when CHEC Kbox the picture when selected, False to display the checkbox when it is selected.

android:state_enabled            If the value is true, The picture is displayed when the component is available, false, and displayed when the component is not available.
 
android:state_window_focused     If the value is true, the picture is displayed when the activity obtains the first focus; Fals E, the picture is displayed when it is not in the front

These property values can also be superimposed, such as:
Android:state_window_focused= "true" android:state_pressed= "true"

Indicates the background picture when the focus is acquired in non-touch mode and clicked.

In general, the button control only needs to be android:state_pressed. For example, the background setting for a button control is:

<?xml version= "1.0" encoding= "Utf-8"? ><selector xmlns:android= "http://schemas.android.com/apk/res/ Android ">     <item android:state_pressed=" true "android:drawable=" @drawable/bg_selected ">        </ item>    <item android:drawable= "@drawable/bg_unselect" >            </item></selector>

This indicates that when the button control is pressed, the background image is bg_selected, and the background image is bg_unselect in other cases (not being clicked or released after clicking).
Selector You can set the color of the text in addition to the background color of the component. For example, when a button control is pressed, the text color on the button changes. For example:

<?xml version= "1.0" encoding= "Utf-8"? ><selector xmlns:android= "http://schemas.android.com/apk/res/ Android ">    <item android:state_pressed=" true "android:color=" #ffffff "></item>    <item Android:color= "#000000" ></item></selector>

That is, the button control has a color of black, but when it is pressed, the color turns white. When released, the color reverts to black.

The above can be set in code like this:

<button   android:layout_width= "match_parent"   android:layout_height= "Wrap_content"   android: background= "@drawable/btn_slector"   android:text= "abc"   android:textcolor= "@drawable/text_selector"   />

In addition to selector, there is a shape that can be used to customize the graphical effect of the control, both of which can be used together.

Android-selector usage

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.