本文章记录ToggleButton的简单使用。
xml示例代码
<ToggleButton
android:id="@+id/ASDivTogglebutton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="true"
android:textOn="@string/ASDivON"
android:textOff="@string/ASDivOFF"/>
MainActivity.java的简单使用
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.ToggleButton;
//以下代码写在MainActivity类中
private ToggleButton ASDivToggleButton;
//ToggleButton
ASDivToggleButton = (ToggleButton)findViewById(R.id.ASDivTogglebutton);
ASDivToggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
//开状态时,isChecked==true
Toast.makeText(getApplicationContext(), "isChecked==true", Toast.LENGTH_LONG).show();
}else{
//关状态时,isChecked==flase
Toast.makeText(MainActivity.this,"isChecked==false", Toast.LENGTH_LONG).show();
}
}
});