How to use the Switch control in Android Studio 2-2

1.3.2 thumbTint property

android:thumbTint="#0000FF"

The effect is shown in Figure 4.

Figure 4 Changed the color of the thumb

As can be seen in Figure 4, the thumbTint property changes the color of the thumb.

1.4 Control length property

The Switch control can be seen as consisting of two parts: the text displayed by the Switch on the left, and the icon of the Switch on the right. The width of the icon on the right can be set through the switchMinWidth property.

android:switchMinWidth="100dp"

At this time, the effect of Switch is shown in Figure 5.

Figure 5 Changing the icon width

2 Response when "state" changes

The setOnCheckedChangeListener() method of the Switch class is used to set the response when the "state" of the control changes. Response when the program listens for the state of the Switch to change from "off" to "on", or from "on" to "off". The framework of the setOnCheckedChangeListener() method is

sw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)

 {

    .......

}

});

Among them, sw is an object of the Switch class and is associated with the Switch control. The setOnCheckedChangeListener() method of the Switch class is called through sw, and the parameter of this method is the object of the OnCheckedChangeLister class, which overrides the onCheckedChanged() method. The first parameter buttonView of the onCheckedChanged() method represents the control whose state has changed; the second parameter isChecked represents the current state, when the state of the Switch changes from "closed" to "open", the value of isChecked is "true"; When the state of the Switch changes from "on" to "off", the value of isChecked is "true". Therefore, in the onCheckedChanged() method, different processing can be performed according to the different values ​​of isChecked.

Guess you like

Origin blog.csdn.net/hou09tian/article/details/124416864