Android ImageButton after clicking the button before the button color changes

Source: Internet
Author: User

In the process of development, often in order to beautify the interface needs, will modify the default appearance of the button, and because the Android button has three states-default, is clicked, is selected. So, if you want to change the appearance of the button, you need to make changes to these three situations, perhaps in the past, we are most likely to think of is the manual monitoring of button selection and click events, and then write code to replace the background of the button, but in Android, we do not need this trouble, Android has long been a good solution for us, that is selector resources. If we want to implement the three background of the button, we just need to create such an XML file in the res/drawable directory:

Selector.xml

?
56789 <selector xmlns:android="http://schemas.android.com/apk/res/android">        <item android:state_window_focused="false"        android:drawable="@drawable/t3"/>        <item android:state_focused="true"        android:drawable="@drawable/t1"/>        <item android:state_pressed="true"        android:drawable="@drawable/t2"/>        <item android:drawable="@drawable/t3"/></selector>

As described in the resource file above, we define three different image resources for each of these behaviors, and then we just need to specify the background resource as drawable/selector in the corresponding button, and we'll do everything we need to do.

1 <ImageButton         android:layout_width="100px"android:layout_height="50px"        android:src="@drawable/selector"/>

This is all so simple, not to write any logic code, Android development is not very convenient ~, the following is :

Default:

Click:

Selected:

Android ImageButton after clicking the button before the button color changes

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.