Android Selector Settings button click Effects (details) and FAQs

Source: Internet
Author: User

button Click Effect Learning is actually relatively easy, this point for developers is also a relatively frequent use of a knowledge point, and it is related to the edit box to get focus when changing the background color, select button selection change font color and so on. These are actually used in the drawable of the Seletor.

Of course, there are many other effects in drawable can be achieved, the specific can refer to the author of another blog:

Android Modify control skins (using drawable resources)


Effect: (show white when not clicked, gray when clicked)



To achieve this effect is very simple, create an XML file in drawable, and then enter two lines of code to solve,




The first line represents the picture that is displayed when clicked, and the second line represents the picture of the initial status display.

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



You can then set the XML file directly in the button's background, with the following code:

Activity_main:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    android: Fitssystemwindows= "true" >    <button        android:layout_width= "match_parent"        android:layout_height= "Wrap_content"        android:text= "button test"        android:background= "@drawable/simple_button_style"        /> </LinearLayout>


Problems:

When you set the click Effect and the initial status effect in selector, the Click does not respond, the error effect and the code are as follows:





The reader can find that the latter only changes the order of the two lines of code compared to the correct code.

This involves the mechanism of seletor selection of images. Once you've chosen the right picture, you won't be judged.


Take the correct code for example, the first is to determine whether the button has been clicked, if not, do not display gray, continue to select, and then to the second row, the second row provides a white background, that is, the display of white.

In the wrong code, the first line has no conditions, that is, directly select White, out of the selection, it will not be clicked after the decision, so the click Effect will not be displayed.



If the author is not clear of the place, welcome to the reader private messages or comments. Readers interested in drawable can refer to the author's other blog:Android to modify the appearance of the control (using drawable resources)

Android Selector Settings button click Effects (details) and FAQs

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.