LoginSignup
0
0

More than 5 years have passed since last update.

Androidのイベントリスナーを使ってToastを出す

Last updated at Posted at 2016-12-31

チェックボックスの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"
        />
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0