Toastクラスを利用してRadioButtonの内容を表示させる
トーストとは?
トーストとは、Androidアプリで画 に短いメッセージを表示するための機能
- String しか表示できない
- 一定時間が経過すると、 フェードアウトする
あの、下にふわって出てくるやつ。
public static Toast makeText(Context context, CharSequence text, int duration)
context:コンテキスト text:トーストに表示する文字列 duration:表示時間
// Toastオブジェクトの生成 Toast toast = Toast.makeText(this, R.string.message, Toast.LENGTH_LONG); // Toastの表示 Toast.show();
実装
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radio_group); RadioButton radioButton = (RadioButton) findViewById(radioGroup.getCheckedRadioButtonId()); radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { RadioButton checkedButton = (RadioButton) findViewById(checkedId); Log.d("MainActivity", checkedButton.getText().toString()); Toast.makeText(MainActivity.this, checkedButton.getText().toString(), Toast.LENGTH_LONG).show(); } });
AndroidでRadioButtonを作る
ラジオボタンで指定した文言をログに出力させる
レイアウトの作成
<RadioGroup android:id="@+id/radio_group" android:layout_width="wrap_content" android:layout_height="wrap_content"> <RadioButton android:id="@+id/radio_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="高坂穂乃果"/> <RadioButton android:id="@+id/radio_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="絢瀬絵里"/> <RadioButton android:id="@+id/radio_3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="南ことり"/> <RadioButton android:id="@+id/radio_4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="園田海未"/> <RadioButton android:id="@+id/radio_5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="星空凛"/> <RadioButton android:id="@+id/radio_6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="西木野真姫"/> <RadioButton android:id="@+id/radio_7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="東條希"/> <RadioButton android:id="@+id/radio_8" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="小泉花陽"/> <RadioButton android:id="@+id/radio_9" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="矢澤にこ"/> </RadioGroup>
選択したボタンの取得
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radio_group); RadioButton radioButton = (RadioButton) findViewById(radioGroup.getCheckedRadioButtonId()); radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { RadioButton checkedButton = (RadioButton) findViewById(checkedId); Log.d("MainActivity", checkedButton.getText().toString()); } }); }
ログ
03-20 09:51:33.603 8544-8544/com.example.hoge.radiobutton D/MainActivity: 高坂穂乃果 03-20 09:51:34.829 8544-8544/com.example.hoge.radiobutton D/MainActivity: 絢瀬絵里 03-20 09:51:35.800 8544-8544/com.example.hoge.radiobutton D/MainActivity: 南ことり 03-20 09:51:37.914 8544-8544/com.example.hoge.radiobutton D/MainActivity: 園田海未
参考
Androidでトグルスイッチ(Switch)ボタンを作成してイベントを取得する
トグルスイッチを作ってみる
Androidでトグルスイッチを作成し、CompoundButton
をつかってイベントを取得してみる。
レイアウト作成
<Switch android:id="@+id/toggle_switch" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:textColor="#3e3e3e" android:text="toggle switch button"/>
CompoundButton を利用してイベントを取得
CompoundButton
は public abstract class CompoundButton extends Button implements Checkable
ってドキュメントに書いてあった。
CompoundButton | Android Developers
CheckBox, RadioButton, Switch, SwitchCompat, ToggleButton とかのイベントは CompoundButton
で取得できるらしい。
OFF/ON のステートを持ったボタンの共通機能を具備した抽象クラスです。抽象クラスなのでこれ自体をそのまま部品として使うことはできません。
ボタンをチューンしよう! このサイト様に書いてあった。ありがとうございます。
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); CompoundButton toggle = (CompoundButton) findViewById(R.id.toggle_switch); toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { Log.d("MainActivity", "ON"); } else { Log.d("MainActivity", "OFF"); } } }); }
ログ
03-20 09:12:09.969 4390-4390/com.example.hoge.aswitch D/MainActivity: OFF 03-20 09:12:11.055 4390-4390/com.example.hoge.aswitch D/MainActivity: ON 03-20 09:12:15.639 4390-4390/com.example.hoge.aswitch D/MainActivity: OFF 03-20 09:12:16.413 4390-4390/com.example.hoge.aswitch D/MainActivity: ON