代码区:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b = (ToggleButton) findViewById(R.id.toggleButton1);
b.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
Toast.makeText(MainActivity.this,isChecked?"On":"Off", Toast.LENGTH_LONG).show(); // isChecked为true则为开。
}
});
}
布局文件区:
<ToggleButton
android:id="@+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="开"
android:textOff="关"
android:text="ToggleButton" />
非常的简单。