Android RadioGroup控件的使用

       在Android开发过程中,我们经常要使用RadioGroup控件,用于支持单项或者多项选择,今天写了一个RadioGroup的例子,贴出来作为自己学习的记录,如果对大家有帮助就更好了,在例子中,我们选择电影类型,然后在一个TextView中展示选择的结果。

       1. RadioGroupActivity

       控件所在的活动的Activity,包含一个RadioGroup组和一个TextView

       2.radio.xml

       RadioGroupActivity的布局文件

RadioGroupActivity.java

package com.augmentum.example;

import com.example.androidexample.R;

import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class RadioGroupActivity extends Activity {

	private RadioGroup radioGroup;
	private TextView textViewChoice;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.radio);
		
		radioGroup=(RadioGroup)findViewById(R.id.radioGroup);
		textViewChoice = (TextView)findViewById(R.id.textViewChoice);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
        {
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                // checkedId is the RadioButton selected
                RadioButton rb=(RadioButton)findViewById(checkedId);
                textViewChoice.setText("You Selected "+rb.getText());
                //Toast.makeText(getApplicationContext(), rb.getText(), Toast.LENGTH_SHORT).show();
            }
        });

	}
}

radio.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#ffc5bdff">
    <TextView
        android:id="@+id/textViewSelection"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Select your favoutire  movie Genre" />
    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <RadioButton
            android:id="@+id/radioButtonActionMovies"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Action Movies"
            android:checked="false" />
        <RadioButton
            android:id="@+id/radioButtonAnimationMovies"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Animation Movies"
            android:checked="false" />
        <RadioButton
            android:id="@+id/radioButtonHorrorMovies"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Horror Movies"
            android:checked="false" />
        <RadioButton
            android:id="@+id/radioButtonComedyMovies"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Comedy Movies"
            android:checked="false" />
        <RadioButton
            android:id="@+id/radioButtonSciFiMovies"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Sci-Fi Movies"
            android:checked="false" />
    </RadioGroup>
    <TextView
        android:id="@+id/textViewChoice"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="You Selected: "
        android:textStyle="bold"/>
</LinearLayout>

运行效果如下:

Android RadioGroup控件的使用_第1张图片



你可能感兴趣的:(android,Android开发,animation,控件,RadioButton)