xml文件:
<Switch
android:id="@+id/open1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="on"
android:textOff="off"
/>
MainActivity代码
Switch open1=(Switch)view.findViewById(R.id.open1);
open1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked) {
Toast.makeText(getContext(),"on1",Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getContext(),"off1",Toast.LENGTH_SHORT).show();
}
}
});