Today we are listening to switches, two switches, ToggleButton and switch.
<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"><ToggleButtonAndroid:id= "@+id/togglebutton"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:texton= "vertical arrangement"Android:textoff= "Horizontal arrangement"android:checked= "true" /><SwitchAndroid:id= "@+id/switcher"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:texton= "vertical arrangement"Android:textoff= "Horizontal arrangement"android:checked= "true"/>
<LinearLayoutAndroid:id= "@+id/test"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"> <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Test button One" /> <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Test button two" /> <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Test button three" /> </LinearLayout></LinearLayout>
The switch controls whether the three buttons are horizontal or vertical, so you need to wrap the three buttons together and use a linear layout as a whole. One does not understand is why to use android:checked= "true"? Is the meaning of whether to be selected ...
PackageCom.example.togglebutton;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.widget.CompoundButton;ImportAndroid.widget.CompoundButton.OnCheckedChangeListener;Importandroid.widget.LinearLayout;ImportAndroid.widget.Switch;ImportAndroid.widget.ToggleButton; Public classMainactivityextendsActivity {ToggleButton ToggleButton; Switch switcher; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); ToggleButton=(ToggleButton) Findviewbyid (R.id.togglebutton); Switcher=(Switch) Findviewbyid (R.id.switcher); FinalLinearLayout test=(LinearLayout) Findviewbyid (r.id.test); Oncheckedchangelistener Listener=NewOncheckedchangelistener () {@Override Public voidoncheckedchanged (Compoundbutton Buttonview,BooleanisChecked) { //TODO auto-generated Method Stub if(isChecked) {test.setorientation (1);//Vertical}Else{test.setorientation (0);//level } } }; Togglebutton.setoncheckedchangelistener (listener); Switcher.setoncheckedchangelistener (listener); }}
This inside let me realize a focus is data field, and ordinary variable ...
Why use ToggleButton and swithcer as data fields? Why is the test not used as part of the data field?
And one of the reasons why test is final?