Click the togglebutton button to change the background image. Click again to restore the background image.

Source: Internet
Author: User

The icon above, R. drawable. register_checked is the selected Image

The icon below, R. drawable. register_unchecked is the unselected Image

By default, the selected result is shown above. Click the button to cancel the selection. click the button again to select again.

Actually, this is two images. The key is that the image must be changed with the button.

At first, I wanted to use the button to implement it. After a long time, I had to press the button and release it. I didn't click it to change the background image.

After a long time, Baidu found the desired effect with togglebutton, and shared it with you here.


Android: texton = ""
Android: textoff = ""

If the text content is not set to null, the default text is displayed on the background image.

Android: Id = "@ + ID/btn_register_sms_confirm"
Android: layout_width = "30dip"
Android: layout_height = "24dip"
Android: texton = ""
Android: textoff = ""
Android: Background = "@ drawable/register_checked"/> <! -- Here is the selected image by default -->


Note when importing a package.

Import Android. widget. compoundbutton. oncheckedchangelistener;

Here is the selected status listening event, not the Click Event

Findviewbyid (R. Id. btn_register_sms_confirm). setoncheckedchangelistener (New oncheckedchangelistener (){

@ Override
Public void oncheckedchanged (compoundbutton buttonview, Boolean ischecked ){
Btnsmsconfirm. setchecked (ischecked );
Btnsmsconfirm. setbackgroundresource (ischecked? R. drawable. register_checked: R. drawable. register_unchecked );

Click the togglebutton button to change the background image. Click again to restore the background image.

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: 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.