2.常用控件:RadioGroup和RadioButton

来源:互联网 发布:淘宝店铺怎么搞双11 编辑:程序博客网 时间:2024/06/05 15:26

常用控件:RadioGroup和RadioButton

一.核心代码:

视图:

在相应的Activity布局文件中声明<RadioGroup/><RadioButton/>

<RadioGroupandroid:id="@+id/genderGroup"    android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:orientation="vertical"    >    <RadioButton    android:id="@+id/femaleButton"     android:layout_width="wrap_content"       android:layout_height="wrap_content"       android:text="@string/female"      />    <RadioButton    android:id="@+id/maleButton"     android:layout_width="wrap_content"       android:layout_height="wrap_content"       android:text="@string/male"      /></RadioGroup>




数据:

Activity中可以通过View android.app.Activity.findViewById(int id)函数返回RadioGroup对象。

Activity中可以通过View android.app.Activity.findViewById(int id)函数返回RadioButton对象。

为RadioGroup对象设置监听器genderGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener())
创建一个匿名内部类并实现android.widget.RadioGroup.OnCheckedChangeListener接口,作为参数传入RadioGroup对象的setOnCheckedChangeListener(OnCheckedChangeListener listener)函数中。

        //通过控件的ID来得到代表控件的对象        genderGroup = (RadioGroup)findViewById(R.id.genderGroup);        femaleButton = (RadioButton)findViewById(R.id.femaleButton);        maleButton = (RadioButton)findViewById(R.id.maleButton);        //为RadioGroup设置监听器,需要注意的是,这里的监听器和Button控件的监听器有所不同        genderGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {public void onCheckedChanged(RadioGroup group, int checkedId) {// TODO Auto-generated method stubif(femaleButton.getId() == checkedId){System.out.println("famale");Toast.makeText(RadioTest.this, "famle", Toast.LENGTH_SHORT).show();}else if(maleButton.getId() == checkedId){System.out.println("male");}}});



二.控件图例:


三.具体应用:

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"    ><RadioGroupandroid:id="@+id/genderGroup"    android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:orientation="vertical"    >    <RadioButton    android:id="@+id/femaleButton"     android:layout_width="wrap_content"       android:layout_height="wrap_content"       android:text="@string/female"      />    <RadioButton    android:id="@+id/maleButton"     android:layout_width="wrap_content"       android:layout_height="wrap_content"       android:text="@string/male"      /></RadioGroup></LinearLayout>


Activity.java文件:


package mars.activity07;import android.app.Activity;import android.os.Bundle;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.Toast;public class RadioTest extends Activity {    /** Called when the activity is first created. *///对控件对象进行声明private RadioGroup genderGroup = null;private RadioButton femaleButton = null;private RadioButton maleButton = null;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.radio);        //通过控件的ID来得到代表控件的对象        genderGroup = (RadioGroup)findViewById(R.id.genderGroup);        femaleButton = (RadioButton)findViewById(R.id.femaleButton);        maleButton = (RadioButton)findViewById(R.id.maleButton);        //为RadioGroup设置监听器,需要注意的是,这里的监听器和Button控件的监听器有所不同        genderGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {public void onCheckedChanged(RadioGroup group, int checkedId) {// TODO Auto-generated method stubif(femaleButton.getId() == checkedId){System.out.println("famale");Toast.makeText(RadioTest.this, "famle", Toast.LENGTH_SHORT).show();}else if(maleButton.getId() == checkedId){System.out.println("male");}}});                    }    }


四.项目文件地址


原创粉丝点击