RadioButton的应用

package com.c;

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

public class RadioButtonDemoActivity extends Activity {
/** Called when the activity is first created. */
private TextView myTextView;
private RadioButton rb1;
private RadioButton rb2;
private RadioButton rb3;
RadioGroup rg;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myTextView=(TextView) findViewById(R.id.textView);
rb1=(RadioButton) findViewById(R.id.radio1);
rb2=(RadioButton) findViewById(R.id.radio2);
rb3=(RadioButton) findViewById(R.id.radio3);
rg=(RadioGroup) findViewById(R.id.radioGroup);
rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if(R.id.radio1==checkedId){
myTextView.setText("您选择的项目是:"+rb1.getText().toString());
Log.d("Test",String.valueOf(rb1.getText()));
}
else if(R.id.radio2==checkedId){
myTextView.setText("您选择的项目是:"+rb2.getText().toString());
Log.d("Test",String.valueOf(rb2.getText()));
}
else if(R.id.radio3==checkedId){
myTextView.setText("您选择的项目是:"+rb3.getText().toString());
Log.d("Test",String.valueOf(rb3.getText()));
}
}
});
}
}

 

main.xml的设计:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />

<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton1" />


<RadioButton
android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton2" />

<RadioButton
android:id="@+id/radio3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton3" />
</RadioGroup>
</LinearLayout>

 

运行的结果:

 

posted on 2014-12-08 13:36  Iitb  阅读(140)  评论(0)    收藏  举报

导航