加入收藏 | 设为首页 | 会员中心 | 我要投稿 开发网_开封站长网 (http://www.0378zz.com/)- 科技、AI行业应用、媒体智能、低代码、办公协同!
当前位置: 首页 > 教程 > 正文

Android开发教程:RadioButton和CheckBox畅聊

发布时间:2021-11-24 16:09:41 所属栏目:教程 来源:互联网
导读:根据云计算网_泰州站长网 Www.0523Zz.Com报道 一.RadioButton单选按钮 RadioButton(单选按钮)在Android开发中应用的非常广泛,比如一些选择项的时候,会用到单选按钮。它是一种单个圆形单选框双状态的按钮,可以选择或不选择。在RadioButton没有被选中时,用
根据云计算网_泰州站长网 Www.0523Zz.Com报道


一.RadioButton单选按钮  
    RadioButton(单选按钮)在Android开发中应用的非常广泛,比如一些选择项的时候,会用到单选按钮。它是一种单个圆形单选框双状态的按钮,可以选择或不选择。在RadioButton没有被选中时,用户能够按下或点击来选中它。但是,与复选框相反,用户一旦选中就不能够取消选中。
 
    实现RadioButton由两部分组成,也就是RadioButton和RadioGroup配合使用.RadioGroup是单选组合框,可以容纳多个RadioButton的容器.在没有RadioGroup的情况下,RadioButton可以全部都选中;当多个RadioButton被RadioGroup包含的情况下,RadioButton只可以选择一个。并用setOnCheckedChangeListener来对单选按钮进行监听
 
下面的具体的例子:
 
MainActivity.java
 
package com.android.radiobutton;  
 
import android.app.Activity;  
import android.os.Bundle;  
import android.widget.RadioGroup;  
import android.widget.Toast;  
 
public class MainActivity extends Activity {  
      
    //声明RadioGroup  
    RadioGroup raGroup1,raGroup2;  
    @Override
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
          
        //通过findViewById获得RadioGroup对象  
        raGroup1=(RadioGroup)findViewById(R.id.radioGroup1);       
        //添加事件监听器  
        raGroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {  
              
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {  
                // TODO Auto-generated method stub  
                if(checkedId==R.id.radioBtn1){                    
                    Toast.makeText(MainActivity.this, "你来自广东省", Toast.LENGTH_LONG).show();  
                }  
                else if(checkedId==R.id.radioBtn2){  
                    Toast.makeText(MainActivity.this, "你来自广西省", Toast.LENGTH_LONG).show();  
                }  
                else{  
                    Toast.makeText(MainActivity.this, "你来自湖南省", Toast.LENGTH_LONG).show();  
                }  
            }  
        });  
        raGroup2=(RadioGroup)findViewById(R.id.radioGroup2);       
        raGroup2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {  
              
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {  
                // TODO Auto-generated method stub  
                if(checkedId==R.id.radioBtn4){                    
                    Toast.makeText(MainActivity.this, "你的性别是男", Toast.LENGTH_LONG).show();  
                }  
                else {  
                    Toast.makeText(MainActivity.this, "你的性别是女", Toast.LENGTH_LONG).show();  
                }  
            }  
        });         
    }  
}
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:layout_width="fill_parent"   
        android:layout_height="wrap_content"   
        android:text="@string/hello1"
        />
        <RadioGroup
            android:id="@+id/radioGroup1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            >
            <RadioButton
                android:id="@+id/radioBtn1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/radioBtn1"
               />
            <RadioButton
                android:id="@+id/radioBtn2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/radioBtn2"
               />
            <RadioButton
                android:id="@+id/radioBtn3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/radioBtn3"
               />
        </RadioGroup>
    <!-- 在两个RadioGroup之间画条横线 -->
    <View   
        android:layout_width="match_parent"   
        android:layout_height="1dp"
        android:background="#ffffff"
        />
    <TextView    
        android:layout_width="fill_parent"   
        android:layout_height="wrap_content"   
        android:text="@string/hello2"
        />
        <RadioGroup
            android:id="@+id/radioGroup2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"    
            >
            <RadioButton
                android:id="@+id/radioBtn4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/radioBtn4"
                android:textColor="#ffffff"
               />
            <RadioButton
                android:id="@+id/radioBtn5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/radioBtn5"
               />
        </RadioGroup>
</LinearLayout>
strings.xml
 
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello1">你来自哪个省</string>
    <string name="hello2">你的性别是</string>
    <string name="app_name">单选按钮测试</string>
    <string name="radioBtn1">广东</string>
    <string name="radioBtn2">广西</string>
    <string name="radioBtn3">湖南</string>
    <string name="radioBtn4">男</string>
    <string name="radioBtn5">女</string>
</resources> 

(编辑:开发网_开封站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读