Details about Android togglebutton

Source: Internet
Author: User

Most people may have used the view togglebutton.

It is easy to use to implement a listener after initialization.

However, when you first enter the interface, togglebutton will display a "off ". Many people may want to change it. togglebutton. settextoff () or togglebutton. settexton () cannot be used to control the off displayed by default at the beginning.

If you want to change the text displayed on the first page, there are two methods:

1. Android: textoff = "" in XML ""

2. InCodeUse togglebutton. settext (""). Remember, this is not settextoff. Why is it settext? Let's analyze the source code.

The following are some of the key source code:

  Public  togglebutton (context, attributeset attrs,  int  defstyle) {
super (context, attrs, defstyle);
typedarray A =
context. obtainstyledattributes (
attrs, Com. android. internal . r. styleable. togglebutton, defstyle, 0 );
mtexton =. gettext (COM. android. internal . r. styleable. togglebutton_texton);
mtextoff =. gettext (COM. android. internal . r. styleable. togglebutton_textoff);
mdisabledalpha =. getfloat (COM. android. internal . r. styleable. togglebutton_disabledalpha, 0.5f );
synctextstate ();
. recycle ();
}

First, we can see the togglebutton constructor. At first glance, I found synctextstate (); so I checked the code of this method and found

 
Private VoidSynctextstate (){
BooleanChecked= Ischecked ();
If(Checked& Mtexton! =Null){
Settext (mtexton );
}Else If(!Checked& Mtextoff! =Null){
Settext (mtextoff );
}
}

As you can see, settext is used by default when it is set for the first time. This is why settext is used in the Code layout instead of settextoff.

Settextoff and settexton will take effect after you click togglebutton.

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.