一手遮天 Android - view(选择类): ToggleButton 基础
一手遮天 Android - view(选择类): ToggleButton 基础
示例如下:
/view/selection/ToggleButtonDemo1.java
/**
* ToggleButton - 双状态按钮
* setChecked(boolean checked) - 设置当前双状态按钮的选中状态
* isChecked() - 获取当前双状态按钮的选中状态
* setTextOn() - 选中状态时,按钮上显示的文本
* setTextOff() - 未选中状态时,按钮上显示的文本
* setOnCheckedChangeListener(OnCheckedChangeListener listener) - 双状态按钮的选中状态发生变化时的回调
*/
package com.webabcd.androiddemo.view.selection;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.ToggleButton;
import com.webabcd.androiddemo.R;
public class ToggleButtonDemo1 extends AppCompatActivity {
private ToggleButton _toggleButton1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_selection_togglebuttondemo1);
_toggleButton1 = (ToggleButton) findViewById(R.id.toggleButton1);
sample();
}
private void sample() {
_toggleButton1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Toast.makeText(ToggleButtonDemo1.this, String.valueOf(isChecked), Toast.LENGTH_SHORT).show();
}
});
}
}
/layout/activity_view_selection_togglebuttondemo1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!--
ToggleButton - 双状态按钮
checked - 选中状态
textOn - 选中状态时,按钮上显示的文本
textOff - 未选中状态时,按钮上显示的文本
-->
<ToggleButton
android:id="@+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:checked="true"
android:textOff="textOff"
android:textOn="textOn" />
</LinearLayout>