Mục lục
Hướng dẫn sử dụng Android RadioGroup & RadioButton
Xem thêm các chuyên mục:
-
1- RadioButton & RadioGroup
-
RadioButton là một view trong Android, nó thường được sử dụng với RadioGroup. Trong đó RadioGroup là một bộ chứa (container) nó có thể chứa các RadioButton, khi bạn check chọn một radio button trong một nhóm, tất cả các radio button khác trong nhóm sẽ bị mất lựa chọn (deselect).
Dưới đây là hình ảnh các RadioButton được nhóm lại trong các nhóm khác nhau. -
-
RadioGroup có thể sắp xếp các RadioButton theo chiều dọc hoặc chiều ngang.
-
-
2- Ví dụ với RadioGroup & RadioButtons
-
Tạo một project có tên AndroidRadioDemo.
-
-
Thiết kế giao diện ứng dụng (Xem SLIDER):
-
-
-
Giao diện ứng dụng:
-
-
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); // Đăng ký sự kiện khi RadioGroup_diffLevel có thay đổi. this.radioGroupDiffLevel.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { doOnDifficultyLevelChanged(group, checkedId); } }); // Khi radio button "Male" có thay đổi. this.radioButtonMale.setOnCheckedChangeListener(new RadioButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { doOnGameCharacterChanged(buttonView,isChecked); } }); // Khi radio button "Male" có thay đổi. this.radioButtonFemale.setOnCheckedChangeListener(new RadioButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { doOnGameCharacterChanged(buttonView,isChecked); } }); // Khi button "Save" bị nhấn. this.buttonSave.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { doSave(); } }); } // Khi radio group "Difficulty Level" có thay đổi. 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(); } } // Khi radio button có thay đổi. private void doOnGameCharacterChanged(CompoundButton buttonView, boolean isChecked) { RadioButton radio =(RadioButton) buttonView; Log.i(LOGTAG, "RadioButton "+ radio.getText()+" : "+ isChecked); } // Khi button "Save" bị nhấn. 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(); } }
-
Chạy ứng dụng:
-