亲宝软件园·资讯

展开

Android RadioGroup和RadioButton控件 Android RadioGroup和RadioButton控件简单用法示例

无忧一生 人气:0
想了解Android RadioGroup和RadioButton控件简单用法示例的相关内容吗,无忧一生在本文为您仔细讲解Android RadioGroup和RadioButton控件的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android,RadioGroup,RadioButton,控件,下面大家一起来学习吧。

本文实例讲述了Android RadioGroup和RadioButton控件简单用法。分享给大家供大家参考,具体如下:

RadioGroup和RadioButton代表的是Android中单选按钮的一种控件,写个简单的代码熟悉一下:

import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
public class Radio extends Activity {
  private TextView myTextView;
  private RadioButton chinaBtn;
  private RadioButton ukBtn;
  private RadioButton usaBtn;
  private RadioGroup rg;
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    //通过ID找到TextView
    myTextView = (TextView) findViewById(R.id.myTextView);
    //通过ID找到RadioButton
    chinaBtn = (RadioButton) findViewById(R.id.china_Button);
    ukBtn = (RadioButton) findViewById(R.id.uk_Button);
    usaBtn = (RadioButton) findViewById(R.id.usa_Button);
    //通过ID找到RadioGroup
    rg = (RadioGroup) findViewById(R.id.rBtnGroup);
    //只要对RadioGroup进行监听
    rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
      @Override
      public void onCheckedChanged(RadioGroup group, int checkedId) {
        // TODO Auto-generated method stub
        if(R.id.china_Button == checkedId){
          myTextView.setText("您选择的国家是:" + chinaBtn.getText().toString());
        }
        else if(R.id.uk_Button == checkedId){
          myTextView.setText("您选择的国家是:" + ukBtn.getText().toString());
        }
        else if(R.id.usa_Button == checkedId){
          myTextView.setText("您选择的国家是:" + usaBtn.getText().toString());
        }
      }
    });
  }
}

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"
  >
  <TextView
    android:id="@+id/myTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello"
  />
  <RadioGroup android:id="@+id/rBtnGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >
    <RadioButton
      android:id="@+id/china_Button"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="中国"
    />
    <RadioButton
      android:id="@+id/uk_Button"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="英国"
    />
    <RadioButton
      android:id="@+id/usa_Button"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="美国"
    />
  </RadioGroup>
</LinearLayout>

效果如下:

希望本文所述对大家Android程序设计有所帮助。

加载全部内容

相关教程
猜你喜欢
用户评论