◆ RadioButtonを使う(失敗例)
本当はRadioButtonを使う場合は、RadioGroupを使ってグループ内でひとつだけ選択する、という書き方にしないといけませんが、ここでは試しにRadioGroupを使わないとどういう結果になるか試してみることにします。
Androidプロジェクトの設定
プロジェクト名: RadioButtonTest1 ビルドターゲット: Android 2.1-update1 アプリケーション名: RadioButtonTest1 パッケージ名: jp.co.triware.samples.RadioButtonTest1 アクティビティーの作成: RadioButtonTest1 最小SDKバージョン: 7
RadioButtonTest1.java
package jp.co.triware.samples.RadioButtonTest1;
import android.app.Activity;
import android.os.Bundle;
public class RadioButtonTest1 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RadioButton
android:id="@+id/select1_rb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio Button 1"
/>
<RadioButton
android:id="@+id/select2_rb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio Button 2"
/>
<RadioButton
android:id="@+id/select3_rb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio Button 3"
/>
</LinearLayout>実行結果初期値は設定していませんから何も選択されていない状態になります。
"Radio Button 1"を選ぶとラジオボタンの中央が緑に変わりました。
続いて"Radio Button 2"を選ぶと、1と2が選択されてしまいました。
このとき"Radio Button 1"をクリックしても選択解除にはなりません。
"Radio Button 3"を選ぶと、結局すべてが選択された状態になってしまいました。
これで「RadioButtonだけではNG」ということがわかったと思います。
◆ RadioGroupとRadioButton
さて、それではRadioGroupを使って、本来の形でRadioButtonを使ってみましょう。
Androidプロジェクトの設定
プロジェクト名: RadioButtonTest1a ビルドターゲット: Android 2.1-update1 アプリケーション名: RadioButtonTest1a パッケージ名: jp.co.triware.samples.RadioButtonTest1a アクティビティーの作成: RadioButtonTest1a 最小SDKバージョン: 7
RadioButtonTest1a.java
package jp.co.triware.samples.RadioButtonTest1a;
import android.app.Activity;
import android.os.Bundle;
public class RadioButtonTest1a extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RadioGroup
android:id="@+id/group_rg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<RadioButton
android:id="@+id/select1_rb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio Button 1"
/>
<RadioButton
android:id="@+id/select2_rb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio Button 2"
/>
<RadioButton
android:id="@+id/select3_rb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio Button 3"
/>
</RadioGroup>
</LinearLayout>RadioGroupの中にRadioButtonを入れます。
実行結果
"Radio Button 1"をクリックした後、"Radio Button 2"をクリックすると、"Radio Button 2"だけが選択された状態になります。"Radio Button 3"も同様です。
◆ 選択されたラジオボタンを認識する
では最後に、どのラジオボタンが選択されているかを認識してメッセージを表示させてみましょう。
Androidプロジェクトの設定
プロジェクト名: RadioButtonTest1b ビルドターゲット: Android 2.1-update1 アプリケーション名: RadioButtonTest1b パッケージ名: jp.co.triware.samples.RadioButtonTest1b アクティビティーの作成: RadioButtonTest1b 最小SDKバージョン: 7
RadioButtonTest1b.java
package jp.co.triware.samples.RadioButtonTest1b;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class RadioButtonTest1bActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Toast myToast = Toast.makeText(this, "", Toast.LENGTH_LONG);
// ラジオグループ
final RadioGroup rgSelect = (RadioGroup)findViewById(R.id.group_rg);
rgSelect.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
// ラジオボタンが変更されると何番が選択されたか表示する
// @Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
myToast.cancel();
switch (checkedId) {
case R.id.select1_rb:
myToast.setText("1番");
break;
case R.id.select2_rb:
myToast.setText("2番");
break;
case R.id.select3_rb:
myToast.setText("3番");
break;
default:
myToast.setText("エラー");
break;
}
myToast.show();
}
});
// ボタン
Button btnWhich = (Button)findViewById(R.id.whichis_btn);
btnWhich.setOnClickListener(new View.OnClickListener() {
// 選択されているラジオボタンを表示する
// 選択されていない場合は1番目のラジオボタンを選択する
// @Override
public void onClick(View v) {
myToast.cancel();
int id = rgSelect.getCheckedRadioButtonId();
if (id != -1) {
RadioButton rbSelected = (RadioButton)findViewById(id);
myToast.setText(rbSelected.getText());
myToast.show();
} else {
Toast.makeText(getApplicationContext(), "何も選択されていません。1番を選択します。", Toast.LENGTH_LONG).show();
// 指定したIDのラジオボタンを選択する
rgSelect.check(R.id.select1_rb);
}
}
});
}
}main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RadioGroup
android:id="@+id/group_rg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<RadioButton
android:id="@+id/select1_rb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio Button 1"
/>
<RadioButton
android:id="@+id/select2_rb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio Button 2"
/>
<RadioButton
android:id="@+id/select3_rb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio Button 3"
/>
</RadioGroup>
<Button
android:id="@+id/whichis_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Which is selected?"
/>
</LinearLayout>選択されているラジオボタンのIDを知るにはRadioGroup.getCheckedRadioButtonId()メソッドを使います。
選択されているラジオボタンが変化した時はonCheckedChanged()が呼ばれますので、その引数で知ることができます。
サンプルでは初期値は設定していませんが、設定したい場合はjavaではRadioGroup.check()を実行するか、以下のようにxmlに"android:checked"を追加します。
<RadioButton
android:id="@+id/select1_rb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio Button 1"
android:checked="true"
/>
実行結果
何も選択されていない状態で"Which is selected?"ボタンを押すと"Radio Button 1"を選択します。
RadioGroup.check()メソッドで選択しますが、これを呼ぶとonCheckedChanged()が呼ばれますので「1番」を表示しています。
"Radio Button 2"や"Radio Button 3"を選ぶとそれぞれ「2番」「3番」を表示します。
選択中のラジオボタンを再度クリックしてもonCheckedChanged()は呼ばれませんので何も表示しません。
"Which is selected?"ボタンを押すと現在選択中のラジオボタンのタイトルを表示します。














0 件のコメント:
コメントを投稿