Use of the Android selector selector

Source: Internet
Author: User

The buttons usually have two pre-and post-click statuses, such as blue before clicking, gray after clicking, and no longer responding to click events.

If you do not use the selector selector, you will need to do the following similar actions in the program when you click

Button1.setbackgroundresource (r.color.material_grey_300); Button1.settextcolor (Getresources (). GetColor ( R.COLOR.MATERIAL_GREY_50)); Button1.setclickable (false);

If you use the selector selector, we can simplify the above operation to

Button1.setenabled (false);

The selector code is as follows:

<selectorxmlns:android= "Http://schemas.android.com/apk/res/android"><ItemAndroid:color= "@color/igame_normal_white_color"android:state_pressed= "true" /><ItemAndroid:color= "@color/igame_actionbar_button_pressed_color"android:state_enabled= "false" /><ItemAndroid:color= "@color/igame_normal_op_color" /></selector>

The specific use is as follows:

Create a new XML file, such as Button_color_selector.xml, and then put the code for the selector block into the file.

In the layout file, set the button's Properties in the Button tab

android:background= "@drawable/button_color_selector"

Using the selector selector simplifies the code and can be reused, setting its properties directly when other components need to be used.

Use of the Android selector selector

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.