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.