zoukankan      html  css  js  c++  java
  • Android笔记(五):Android中的Radio

    原文地址:http://irving-wei.iteye.com/blog/1076097

    上篇介绍了CheckBox,这节,将接触到的是RadioGroup和RadioButton。 

    它们的关系是:一个RadioGroup对应多个RadioButton,而一个RadioGroup中的RadioButton只能同时有一个被选中,它的选中值就是该RadioGroup的选中值。 


    这一节的代码运行效果图如下所示: 

     

    具体的代码编写过程如下: 
    首先在strings.xml中添加本程序所要用到的字符串: 

    Xml代码  收藏代码
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <resources>  
    3.     <string name="hello">Hello World, Test!</string>  
    4.     <string name="app_name">AndroidWithRadioGroup</string>  
    5.     <string name="radio_1">帅哥</string>  
    6.     <string name="radio_2">美女</string>  
    7.     <string name="question">请问你的性别是?</string>  
    8. </resources>  



    接下来就是在main.xml中添加一个显示信息的TextView和一个RadioGroup,该RadioGroup包含两个RadioButton,代码如下: 

    Xml代码  收藏代码
    1. <TextView    
    2.     android:id="@+id/showText"  
    3.     android:layout_width="228px"   
    4.     android:layout_height="49px"   
    5.     android:text="@string/question"  
    6.     android:textSize="20sp"  
    7.     />  
    8.       
    9.     <!-- 建立一个radioGroup -->  
    10. <RadioGroup   
    11.     android:id="@+id/radioGroup"  
    12.     android:layout_width="137px"  
    13.     android:layout_height="216px"  
    14.     android:orientation="horizontal"  
    15. >  
    16.   
    17.     <!-- 建立一个RadioButton -->  
    18. <RadioButton  
    19.     android:id="@+id/radioButton1"  
    20.     android:layout_width="wrap_content"  
    21.     android:layout_height="wrap_content"  
    22.     android:text="@string/radio_1"  
    23. />        
    24.   
    25.     <!-- 建立第二个RadioButton -->  
    26. <RadioButton   
    27.     android:id="@+id/radioButton2"  
    28.     android:layout_width="wrap_content"  
    29.     android:layout_height="wrap_content"  
    30.     android:text="@string/radio_2"  
    31. />     
    32.   
    33. </RadioGroup>  



    接下来就是在Activity的子类中,获取到以上定义的三个组件,而后给RadioGroup添加上OnCheckedChangeListener并实现监听方法。 
    代码如下: 

    Java代码  收藏代码
      1. super.onCreate(savedInstanceState);  
      2. setContentView(R.layout.main);  
      3.   
      4. textView = (TextView) findViewById(R.id.showText);  
      5. radioGroup = (RadioGroup) findViewById(R.id.radioGroup);  
      6. radioButton1 = (RadioButton) findViewById(R.id.radioButton1);  
      7. radioButton2 = (RadioButton) findViewById(R.id.radioButton2);  
      8.   
      9. radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {  
      10.   
      11.     public void onCheckedChanged(RadioGroup group, int checkedId) {  
      12.         if (checkedId == radioButton1.getId()) {  
      13.             textView.setText(radioButton1.getText());  
      14.         } else if (checkedId == radioButton2.getId()) {  
      15.             textView.setText(radioButton2.getText());  
      16.         }  
      17.     }  
      18. });  
  • 相关阅读:
    特征词选择算法对文本分类准确率的影响(前言)
    答火星人.NET。如何使用我的本科毕业程序 正文提取DEMO
    有关matlab画图格式的部分代码
    文本分类中的特征词选择算法系列科普(前言AND 一)
    c++杂项备忘
    写一点应用关于 Lucene.Net,snowball的重新组装(一)在Lucene.Net中加入词性标注与词根还原功能
    C++字符串处理:批量去重,以及大写变小写
    Python打印到文件
    中文分词:采用二元词图以及viterbi算法(三)
    博客园和百度空间,我的两个家
  • 原文地址:https://www.cnblogs.com/kevincode/p/3898006.html
Copyright © 2011-2022 走看看