チェックボックスのOn-Offを切り替えてToastを出す
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CheckBox chk = (CheckBox)findViewById(R.id.chk);
chk.setOnCheckedChangeListener(
new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){
Toast.makeText(
MainActivity.this,
isChecked ? "メール送信" : "メール送信をオフ",
Toast.LENGTH_SHORT
).show();
}
}
);
}
}
activity_main
<ToggleButton
android:id="@+id/chk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="メール通知"
/>
トグルボタンのOn-Offを切り替えてToastを出す
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ToggleButton chk = (ToggleButton)findViewById(R.id.chk);
chk.setOnCheckedChangeListener(
new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){
Toast.makeText(
MainActivity.this,
isChecked ? "メール送信" : "メール送信をオフ",
Toast.LENGTH_SHORT
).show();
}
}
);
}
}
activity_main
<ToggleButton
android:id="@+id/chk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:textOff="メール送信はオフ"
android:textOn="メール送信はオン"
/>
SwitchのOn-Offを切り替えてToastを出す
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.Toast;
import android.widget.ToggleButton;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Switch chk = (Switch)findViewById(R.id.chk);
chk.setOnCheckedChangeListener(
new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){
Toast.makeText(
MainActivity.this,
isChecked ? "メール送信" : "メール送信をオフ",
Toast.LENGTH_SHORT
).show();
}
}
);
}
}
activity_main
<Switch
android:id="@+id/chk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:showText="true"
android:text="メール送信?"
android:textOff="いいえ"
android:textOn="はい"
android:checked="true"
/>