发布于 2017-03-26 02:28:34 | 99 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Android移动端操作系统

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。


这篇文章主要介绍了Android RadioGroup和RadioButton控件简单用法,结合实例形式分析了Android单选按钮控件的基本定义、布局与功能实现技巧,需要的朋友可以参考下

本文实例讲述了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程序设计有所帮助。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务