Androidラジオボタンの例
Androidでは、 “http://developer.android.com/reference/android/widget/RadioButton.html[android.widget.RadioButton]”クラスを使用してラジオボタンをレンダリングできます。これらのラジオボタンは、通常、http://developer.android.com/reference/android/widget/RadioGroup.html[android.widget.RadioGroup]
RadioButtons`がグループ内にある場合、グループ内の1つの
RadioButton`が選択されると、他の全ては自動的に選択解除されます。
このチュートリアルでは、XMLを使用して2つのラジオボタンを作成し、ラジオグループにグループ化する方法を示します。ボタンをクリックすると、どのラジオボタンが選択されているかを表示します。
P.SこのプロジェクトはEclipse 3.7で開発され、Android 2.3.3.
でテストされています
1.カスタム文字列
”
res/values/strings.xml
“ファイルを開き、ラジオボタン用のカスタム文字列を追加します。
File:res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, MyAndroidAppActivity!</string>
<string name="app__name">MyAndroidApp</string>
<string name="radio__male">Male</string>
<string name="radio__female">Female</string>
<string name="btn__display">Display</string>
</resources>
2. RadioButton
”
res/layout/main.xml
“ファイルを開き、 ”
RadioGroup
“、 ”
RadioButton
“とボタンをLinearLayout内に追加します。
ファイル:res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout__width="fill__parent"
android:layout__height="fill__parent"
android:orientation="vertical" >
<RadioGroup
android:id="@+id/radioSex"
android:layout__width="wrap__content"
android:layout__height="wrap__content" >
<RadioButton
android:id="@+id/radioMale"
android:layout__width="wrap__content"
android:layout__height="wrap__content"
android:text="@string/radio__male"
android:checked="true"/>
<RadioButton
android:id="@+id/radioFemale"
android:layout__width="wrap__content"
android:layout__height="wrap__content"
android:text="@string/radio__female"/>
</RadioGroup>
<Button
android:id="@+id/btnDisplay"
android:layout__width="wrap__content"
android:layout__height="wrap__content"
android:text="@string/btn__display"/>
</LinearLayout>
3.コードコード
アクティビティの “ onCreate() `”メソッドの中で、ボタン上にクリックリスナを添付してください。
ファイル:私のAndroid App Activity.java
package com.mkyong.android;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MyAndroidAppActivity extends Activity {
private RadioGroup radioSexGroup;
private RadioButton radioSexButton;
private Button btnDisplay;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
}
public void addListenerOnButton() {
radioSexGroup = (RadioGroup) findViewById(R.id.radioSex);
btnDisplay = (Button) findViewById(R.id.btnDisplay);
btnDisplay.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//get selected radio button from radioGroup
int selectedId = radioSexGroup.getCheckedRadioButtonId();
//find the radiobutton by returned id
radioSexButton = (RadioButton) findViewById(selectedId);
Toast.makeText(MyAndroidAppActivity.this,
radioSexButton.getText(), Toast.LENGTH__SHORT).show();
}
});
}
}
4.デモ
アプリケーションを実行します。
{空} 1。結果、ラジオオプション「男性」が選択されます。

{空} 2。 「女性」を選択し、「表示」ボタンをクリックすると、選択されたラジオボタンの値が表示されます。

ソースコードをダウンロードする
ダウンロードする –
Android-RadioButton-Example.zip
(15 KB)
参考文献
RadioGroup JavaDoc]。
http://developer.android.com/reference/android/widget/RadioButton.html
[Android
RadioButton JavaDoc]。
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/RadioGroup1.html
[Android
ラジオグループの例]。
http://developer.android.com/resources/tutorials/views/hello-formstuff.html#RadioButtons
[Android
ラジオボタンの例]
リンク://タグ/アンドロイド/[アンドロイド]リンク://タグ/ラジオボタン/[ラジオボタン]