English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
This article describes the usage of Android programming single option box RadioGroup. Share it with everyone for reference, as follows:
Today, we introduce the group event of RadioGroup. RadioGroup can limit each different RadioButton to the same Radio button group, and the buttons in the same RadioGroup group can only make a single choice (single choice question).
Firstly, we design a TextView Widget and a RadioGroup, and place two RadioButton inside the RadioGroup, defaulting to no selection. During the program's execution phase, use onCheckedChanged as the trigger event, allowing the User to select one button, displaying the selected content, and finally displaying the RadioButton's option text in the TextView.
Next, let's take a look at the effect diagram:
Below is the relevant code involved:
string.xml:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, RadioGroupDemo</string> <string name="app_name">RadioGroupDemo</string> <string name="tr_radio_op1"> Handsome man</string> <string name="tr_radio_op2">Beautiful girl</string> <string name="str_radio_question1">Please ask you are? </string> </resources>
Main layout main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > !--The first TextView --> <TextView android:id="@"+id/myTextView" android:layout_width="228px" android:layout_height="49px" android:text="@string/str_radio_question1" android:textSize="30sp" /> !--Establish a RadioGroup --> <RadioGroup android:id="@"+id/myRadioGroup" android:layout_width="137px" android:layout_height="216px" android:orientation="vertical" > !--First RadioButton --> <RadioButton android:id="@"+id/myRadioButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/tr_radio_op1" /> !--Second RadioButton --> <RadioButton android:id="@"+id/myRadioButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/tr_radio_op2" /> </RadioGroup> </LinearLayout>
Finally, it is the main control program RadioGroupDemo.Java:
package com.android.test; import android.app.Activity; import android.os.Bundle; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.TextView; public class RadioGroupDemo extends Activity { public TextView mTextView1; public RadioGroup mRadioGroup1; public RadioButton mRadio1,mRadio2; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); /*Get TextView, RadioGroup, RadioButton objects*/ mTextView1 = (TextView) findViewById(R.id.myTextView); mRadioGroup1 = (RadioGroup) findViewById(R.id.myRadioGroup); mRadio1 = (RadioButton) findViewById(R.id.myRadioButton1); mRadio2 = (RadioButton) findViewById(R.id.myRadioButton2); /*RadioGroup uses OnCheckedChangeListener to run*/ mRadioGroup1.setOnCheckedChangeListener(mChangeRadio); } private RadioGroup.OnCheckedChangeListener mChangeRadio = new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { // TODO Auto-generated method stub if(checkedId==mRadio1.getId()) { /*Set mRadio1and pass the content to mTextView1*/ mTextView1.setText(mRadio1.getText()); } else if(checkedId==mRadio2.getId()) { /*Set mRadio2and pass the content to mTextView1*/ mTextView1.setText(mRadio2.getText()); } } }; }
Run RadioGroupDemo.java, and you will get the above effect.
Readers who are interested in more content related to Android can check the special topics on this site: 'Summary of Android View View Techniques', 'Summary of Android Layout Techniques', 'Summary of Android Graphics and Image Processing Techniques', 'Android Development Tutorial for Beginners and Advanced', 'Summary of Android Debugging Techniques and Common Problem Solutions', 'Summary of Android Multimedia Operation Techniques (audio, video, recording, etc.)', 'Summary of Android Basic Component Usage', and 'Summary of Android Widget Usage'
I hope the content described in this article will be helpful to everyone in Android program design.
Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#w3Please report by email to codebox.com (replace # with @ when sending an email) and provide relevant evidence. Once verified, this site will immediately delete the infringing content.