Die Anleitung zu Android RadioGroup & RadioButton

1- RadioButton & RadioGroup

RadioButton ist ein view im  Android. Er ist normaleweise mit RadioGroup benutzt. Davon ist RadioGroup ein Erhalter (container), die die  RadioButton enthalten können. Wenn Sie auf einen Radiobutton in einer Gruppe zu wählen klicken, werden alle anderen Radionbutton in der Gruppe auswählen (deselect).

Unten ist das Bild von RadioButton , die in der unterschiedlichen Gruppe gesetzt werden
RadioGroup kann die  RadioButton nach der horizontalen oder vertikalen Richtung arrangieren

2- Das Beispiel mit RadioGroup & RadioButtons

Erstellen Sie ein Projekt mit dem Name von  AndroidRadioDemo.
  • Bauen Sie die Interface der Applikation (sehen Sie bitte die SLIDER):

Die Interface der Applikation
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="org.o7planning.androidradiodemo.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Game Settings"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Game character"
        android:id="@+id/textView2"
        android:layout_below="@+id/textView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="39dp" />

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_below="@+id/textView2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="38dp"
        android:id="@+id/radioGroup_character">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Male"
            android:id="@+id/radioButton_male"
            android:checked="true" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Female"
            android:id="@+id/radioButton_female"
            android:checked="false" />
    </RadioGroup>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Difficulty Level"
        android:id="@+id/textView3"
        android:layout_below="@+id/radioGroup_character"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="10dp" />

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_below="@+id/textView3"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="15dp"
        android:orientation="horizontal"
        android:id="@+id/radioGroup_diffLevel">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Easy"
            android:id="@+id/radioButton_easy"
            android:checked="false" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Medium"
            android:id="@+id/radioButton_medium"
            android:checked="true" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hard"
            android:id="@+id/radioButton_hard"
            android:checked="false" />
    </RadioGroup>

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Save"
        android:id="@+id/button_save"
        android:layout_below="@+id/radioGroup_diffLevel"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="41dp" />

</RelativeLayout>
MainActivity.java
package org.o7planning.androidradiodemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

   private RadioGroup radioGroupCharacter;
   private RadioButton radioButtonMale;
   private RadioButton radioButtonFemale;

   private RadioGroup radioGroupDiffLevel;

   private Button buttonSave;


   private String LOGTAG = "AndroidRadioDemo";



   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

       //
       this.radioGroupCharacter= (RadioGroup) this.findViewById(R.id.radioGroup_character);
       this.radioButtonMale = (RadioButton) this.findViewById(R.id.radioButton_male);
       this.radioButtonFemale  =  (RadioButton)this.findViewById(R.id.radioButton_female);

       this.radioGroupDiffLevel= (RadioGroup) this.findViewById(R.id.radioGroup_diffLevel);

       this.buttonSave= (Button) this.findViewById(R.id.button_save);

       // When radio group "Difficulty Level" checked change.
       this.radioGroupDiffLevel.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
           @Override
           public void onCheckedChanged(RadioGroup group, int checkedId) {
                doOnDifficultyLevelChanged(group, checkedId);
           }
       });

       // When radio button "Female" checked change.
       this.radioButtonMale.setOnCheckedChangeListener(new RadioButton.OnCheckedChangeListener() {

           @Override
           public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
               doOnGameCharacterChanged(buttonView,isChecked);
           }
       });

       // When radio button "Male" checked change.
       this.radioButtonFemale.setOnCheckedChangeListener(new RadioButton.OnCheckedChangeListener() {

           @Override
           public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
               doOnGameCharacterChanged(buttonView,isChecked);
           }
       });

       // When button "Save" clicked.
       this.buttonSave.setOnClickListener(new Button.OnClickListener() {
           @Override
           public void onClick(View v) {
               doSave();
           }
       });
   }


   // When radio group "Difficulty Level" checked change.
   private void doOnDifficultyLevelChanged(RadioGroup group, int checkedId) {
       int checkedRadioId = group.getCheckedRadioButtonId();

       if(checkedRadioId== R.id.radioButton_easy) {
           Toast.makeText(this,"You choose the level of difficulty: Easy",Toast.LENGTH_SHORT).show();
       } else if(checkedRadioId== R.id.radioButton_medium ) {
           Toast.makeText(this,"You choose the level of difficulty: Medium",Toast.LENGTH_SHORT).show();
       } else if(checkedRadioId== R.id.radioButton_hard) {
           Toast.makeText(this,"You choose the level of difficulty: Hard",Toast.LENGTH_SHORT).show();
       }
   }

   // When radio button checked change.
   private void doOnGameCharacterChanged(CompoundButton buttonView, boolean isChecked)  {
       RadioButton radio =(RadioButton) buttonView;

       Log.i(LOGTAG, "RadioButton "+ radio.getText()+" : "+ isChecked);
   }

   // When button "Save" clicked.
   private void doSave()  {
       int difficultyLevel = this.radioGroupDiffLevel.getCheckedRadioButtonId();
       int gameCharacter = this.radioGroupCharacter.getCheckedRadioButtonId();

       RadioButton radioButtonDiffLevel = (RadioButton) this.findViewById(difficultyLevel);
       RadioButton radioButtonGameCharacter = (RadioButton) this.findViewById(gameCharacter);

       String message ="Difficulty Level: "+ radioButtonDiffLevel.getText()
                    +", Game Character: " + radioButtonGameCharacter.getText() ;

       Toast.makeText(this,message,Toast.LENGTH_LONG).show();
   }
}
Die Applikation laufen

View more categories: