ToggleButton

<ToggleButton
    android:id="@+id/toggle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textOn="开"
    android:textOff="关"
    android:checked="true" />
private ToggleButton toggle;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    toggle = (ToggleButton) findViewById(R.id.toggle);
    toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            String str = "";
            if(isChecked){
                str = "开";
            }else{
                str = "关";
            }
            Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG).show();
        }
    });
}
原文地址:https://www.cnblogs.com/anni-qianqian/p/5430534.html