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
参考
Android で CheckBox を作成してみる
CheckBoxを用意し、結果をログに表示させる。
Android の CheckBox を作成して、ON/OFF をログに表示させるところまで実装をしてみる。
レイアウトの作成
<CheckBox android:id="@+id/checkbox" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="メールマガジン受信" />
クリックイベントの取得
setOnClickListener
でイベント取得する。CompoundButton
のほうがきれいに書けるのかな?
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.checkbox).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { CheckBox checkBox = (CheckBox) v; if (checkBox.isChecked()) { Log.d("MainActivity", "メールマガジン受信ON"); } else { Log.d("MainActivity", "メールマガジン受信OFF"); } } }); }
ログの表示
03-20 09:05:20.637 1651-1651/com.example.hoge.checkbox D/MainActivity: メールマガジン受信ON 03-20 09:05:22.674 1651-1651/com.example.hoge.checkbox D/MainActivity: メールマガジン受信OFF 03-20 09:05:24.047 1651-1651/com.example.hoge.checkbox D/MainActivity: メールマガジン受信ON 03-20 09:05:27.760 1651-1651/com.example.hoge.checkbox D/MainActivity: メールマガジン受信OFF