- 继承
java.lang.Object
↳ android.view.View
↳ android.widget.TextView
↳ android.widget.Button
↳ android.widget.CompoundButton
↳ android.widget.ToggleButton - android:disabledAlpha=“xx” 禁用时应用于指示器的alpha
- android:textOff=“xx” 未选中时,按钮的内容
- android:textOn=“xx” 选中时按钮的内容
- android:textAllCaps=“false” 控件显示的英文,是否全部大写, true为大写,fasle为小写
public class MainActivity extends AppCompatActivity {
private ToggleButton tb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initClick();
}
private void initView() {
tb = findViewById(R.id.tb);
}
private void initClick() {
tb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
Toast.makeText(MainActivity.this, buttonView.getText().toString(), Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this, buttonView.getText().toString(), Toast.LENGTH_SHORT).show();
}
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
tools:context=".MainActivity">
<ToggleButton
android:id="@+id/tb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:disabledAlpha="1.5"
android:textOff="中文"
android:checked="false"
android:textAllCaps="false"
android:textOn="English" />
</LinearLayout>