Android imagebutton: how to display different images between the selected and unselected statuses

Source: Internet
Author: User

1. Configure the imagebutton defined in the following layout in XML.

<Imagebutton
Android: Id = "@ ID/rte_bold"
Android: layout_width = "40dip"
Android: layout_height = "40dip"
Android: src = "@ drawable/rte_bold_state"
Android: scaletype = "fitxy"
Android: layout_margintop = "5dip"
Android: textsize = "10dip"/>

2. The rte_bold_state layout XML below, in different States ("selected" and "unselected") show different drawbles.

<? XML version = "1.0" encoding = "UTF-8"?>
<Selector xmlns: Android = "http://schemas.android.com/apk/res/android">
<Item Android: state_selected = "true" Android: drawable = "@ drawable/rte_bold_selected"/>
<Item Android: drawable = "@ drawable/rte_bold"/>
</Selector>

3. the following code controls whether the image is selected to change the image:

Boldbutton = (imagebutton) view. findviewbyid (R. Id. rte_bold );

Boldbutton. setonclicklistener (New onclicklistener (){

@ Override
Public void onclick (view v ){

If (boldbutton. isselected ()){
Boldbutton. setselected (false );
}
Else {
Boldbutton. setselected (true );
}

});

If you have any questions, please tell me that I am an android cainiao.

Related Article

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.