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.
XML:
Android: texton = ""
Android: textoff = ""
If the text content is not set to null, the default text is displayed on the background image.
<Togglebutton
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 -->
Java:
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.