In Android können Sie die Klasse "http://developer.android.com/reference/android/widget/RadioButton.html[android.widget.RadioButton]" verwenden, um das Optionsfeld zu rendern. Diese Optionsfelder werden normalerweise nach http gruppiert://developer.android.com/reference/android/widget/RadioGroup.html[android.widget.RadioGroup].
Wenn sich "RadioButtons" in einer Gruppe befinden, wird bei Auswahl eines "RadioButton" innerhalb einer Gruppe alle anderen automatisch deaktiviert.
In diesem Lernprogramm zeigen wir Ihnen, wie Sie mit XML zwei Optionsfelder erstellen und in eine Optionsgruppe gruppieren. Wenn Sie auf die Schaltfläche klicken, wird angezeigt, welche Optionsschaltfläche ausgewählt ist.
__P.S Dieses Projekt wurde in Eclipse 3.7 entwickelt und mit Android 2.3.3 getestet.
1. Benutzerdefinierte Zeichenfolge
Öffnen Sie die Datei " res/values /strings.xml " und fügen Sie einen benutzerdefinierten String für das Optionsfeld hinzu.
Datei: 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
Öffnen Sie die Datei "
res/layout/main.xml
", fügen Sie "
RadioGroup
", "
RadioButton
" und eine Schaltfläche in das
LinearLayout
Datei: 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. Code Code
Hängen Sie in der Aktivitätsmethode "onCreate ()" einen Klicklistener an.
Datei: Meine 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. Demo
Führen Sie die Anwendung aus.
{leer} 1. Ergebnis: Die Option "männlich" ist ausgewählt.
{leer} 2. Wählen Sie „Female“ und klicken Sie auf die Schaltfläche „Display“. Der ausgewählte Optionsfeldwert wird angezeigt.
Quellcode herunterladen
Download it - Android-RadioButton-Example.zip (15 KB)
Referenzen
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
RadioGroup-Beispiel]. http://developer.android.com/resources/tutorials/views/hello-formstuff.html#RadioButtons [Android
RadioButtons Beispiel]