ZNDS智能电视网 推荐当贝市场

TV应用下载 / 资源分享区

软件下载 | 游戏 | 讨论 | 电视计算器

综合交流 / 评测 / 活动区

交流区 | 测硬件 | 网站活动 | Z币中心

新手入门 / 进阶 / 社区互助

新手 | 你问我答 | 免费刷机救砖 | ROM固件

查看: 15464|回复: 0
上一主题 下一主题
[教程]

Android基础教程(七)之----单选项框RadioGroup的综合应用

[复制链接]
跳转到指定楼层
楼主
发表于 2013-8-28 16:30 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
>    
下面是涉及的相关代码:
   
   
  1. <?xml version="1.0" encoding="utf-8"?>   
    <resources>   
      <string name="hello">Hello World, RadioGroupDemo</string>   
      <string name="app_name">RadioGroupDemo</string>   
      <string name="tr_radio_op1">帅哥</string>   
      <string name="tr_radio_op2">美女</string>   
      <string name="str_radio_question1">请问你是?</string>   
    </resources>
复制代码
  
   
主布局
   
   
  1. <?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 -->   
      <TextView   
        android:id="@+id/myTextView"   
        android:layout_width="228px"   
        android:layout_height="49px"   
        android:text="@string/str_radio_question1"   
        android:textSize="30sp"      
      />      
      <!--建立一個RadioGroup -->   
      <RadioGroup   
        android:id="@+id/myRadioGroup"   
        android:layout_width="137px"   
        android:layout_height="216px"   
        android:orientation="vertical"   
        >   
        <!--第一個RadioButton -->   
        <RadioButton   
          android:id="@+id/myRadioButton1"   
          android:layout_width="wrap_content"   
          android:layout_height="wrap_content"   
          android:text="@string/tr_radio_op1"   
        />   
        <!--第二個RadioButton -->   
        <RadioButton   
          android:id="@+id/myRadioButton2"   
          android:layout_width="wrap_content"   
          android:layout_height="wrap_content"   
          android:text="@string/tr_radio_op2"   
        />   
        </RadioGroup>        
    </LinearLayout>
复制代码
  
   
最后是主控制程序
   
   
  1. package com.android.test;   
       
    import android.app.Activity;   
    import android.os.Bundle;   
    import android.widget.RadioButton;   
    import android.widget.RadioGroup;   
    import android.widget.TextView;   
       
    public class RadioGroupDemo extends Activity   
    {   
      public TextView mTextView1;   
      public RadioGroup mRadioGroup1;   
      public RadioButton mRadio1,mRadio2;   
         
      public void onCreate(Bundle savedInstanceState)   
      {   
        super.onCreate(savedInstanceState);   
        setContentView(R.layout.main);   
            
        /*取得 TextView、RadioGroup、RadioButton对象*/   
        mTextView1 = (TextView) findViewById(R.id.myTextView);   
        mRadioGroup1 = (RadioGroup) findViewById(R.id.myRadioGroup);   
        mRadio1 = (RadioButton) findViewById(R.id.myRadioButton1);   
        mRadio2 = (RadioButton) findViewById(R.id.myRadioButton2);   
             
        /*RadioGroup用OnCheckedChangeListener来运行*/   
        mRadioGroup1.setOnCheckedChangeListener(mChangeRadio);   
      }   
          
      private RadioGroup.OnCheckedChangeListener mChangeRadio = new   
               RadioGroup.OnCheckedChangeListener()   
      {   
        @Override   
        public void onCheckedChanged(RadioGroup group, int checkedId)   
        {   
          // TODO Auto-generated method stub   
          if(checkedId==mRadio1.getId())   
          {   
            /*把mRadio1的内容传到mTextView1*/   
            mTextView1.setText(mRadio1.getText());   
          }   
          else if(checkedId==mRadio2.getId())   
          {   
            /*把mRadio2的内容传到mTextView1*/   
            mTextView1.setText(mRadio2.getText());   
          }         
        }   
      };   
    }
复制代码
  
   运行 ,将得到以上效果...今天就到这~</div

上一篇:《Android Dev Guide》系列教程4:应用基础之Activities 和任务
下一篇:Android基础教程(八)AutoCompleteTextView的应用
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|新帖|标签|软件|Sitemap|ZNDS智能电视网 ( 苏ICP备2023012627号 )

网络信息服务信用承诺书 | 增值电信业务经营许可证:苏B2-20221768 丨 苏公网安备 32011402011373号

GMT+8, 2025-5-21 22:39 , Processed in 0.134618 second(s), 15 queries , Redis On.

Powered by Discuz!

监督举报:report#znds.com (请将#替换为@)

© 2007-2025 ZNDS.Com

快速回复 返回顶部 返回列表