Le Tutoriel de Android RadioGroup & RadioButton

View more categories:

1- RadioButton & RadioGroup

RadioButton est un  view dans  Android, il est souvent utilisé avec  RadioGroup. Dans lequel  RadioGroup est un conteneur (container) comprenant des  RadioButton, lorsque vous cochez et choisissez un bouton radio en groupe, tous les autres boutons radio du groupe seront désélectionnés(deselect).

Ci- dessous sont des images de  RadioButton qui ont regroupé dans différents groupes.
RadioGroup peut exposer des  RadioButton horizontallement ou verticalement.

2- L'exemple avec RadioGroup & RadioButtons

Créez le projet baptisé  AndroidRadioDemo.
  • L'interface de l'application (Voir SLIDER):
L'interface de l'application:
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();
   }
}
L'exécution de l'application:

View more categories: