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. });  
  • 相关阅读:
    SharePoint
    北京的雨
    SharePoint2003安装问题解答
    SharePoint安装时“Service Unavailable”的解决办法
    SharePoint安装程序常见问题解答和故障排查指南,做个参考:)
    银弹的看法
    vue组件注册方式
    vue中template和render的区别
    前端需要注意的SEO
    公众号中H5页面分享给好友或朋友圈自定义图片或文字
  • 原文地址:https://www.cnblogs.com/kevincode/p/3898006.html
Copyright © 2011-2022 走看看