zoukankan      html  css  js  c++  java
  • 03 RadioButton 单选按钮


           >概念:从多个互斥选项中选择一个  如果是选项全部展开  RadioButton  不是展开的Spinner(下拉列表)

        >属性: android:checked="true"

        >使用方法:
             使用RadioButton要用RadioGroup进行分组 RadioGroup是LinearLayout的子类  可以控制方向
            >方式一:使用onclickListner 监听事件(点击事件)

            >方式二:****使用OnCheckedChangeListener (RadioGroup)  状态改变的监听 *****


    package com.fmy.a;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.CompoundButton;
    import android.widget.RadioButton;
    import android.widget.RadioGroup;
    import android.widget.RadioGroup.OnCheckedChangeListener;
    import android.widget.Toast;
    
    public class MainActivity extends Activity {
    
    
    	private RadioGroup rg;
    	private RadioButton zrf;
    	private RadioButton zxc;
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.demo_textview);
    		rg = (RadioGroup) findViewById(R.id.rg);
    		zrf = (RadioButton) findViewById(R.id.zrf);
    		zxc = (RadioButton) findViewById(R.id.zxc);
    		zxc.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    			
    			@Override    //buttonView就是RadioButton对象 isChecked对象是否本选中
    			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    				Toast.makeText(MainActivity.this, "嘿嘿"+buttonView.getText().toString(), 3).show();
    			}
    
    			
    		});
    		rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    			//rg本身的对象 checkedId 状态被改变子id
    			@Override
    			public void onCheckedChanged(RadioGroup group, int checkedId) {
    				RadioButton rb =(RadioButton) findViewById(checkedId);
    				Toast.makeText(MainActivity.this, "哈哈"+rb.getText().toString(), 3).show();
    			}
    		});
    	}
    	
    	
    	
    
    }
    

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
        <RadioGroup
            android:id="@+id/rg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
    <!--     android:checked="true" 设置默认选中-->
            <RadioButton
                android:id="@+id/zrf"
                android:checked="true"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="周润发" />
    
            <RadioButton
                android:id="@+id/zxc"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="周星驰" />
        </RadioGroup>
    
    </LinearLayout>


  • 相关阅读:
    Web负载均衡的几种实现方式
    Apache和Nginx的区别
    Nginx和Apache区别
    Git 使用中显示“Another git process seems to be running in this repository...”问题解决
    上传本地代码到gitHub过程详解
    MySQL数据库中varchar与char类型的区别
    正则表达式中/i,/g,/ig,/gi,/m的区别和含义
    内行看门道:看似“佛系”的《QQ炫舞手游》,背后的音频技术一点都不简单
    惧怕羊毛党?腾讯云为你保驾护航
    教你1天搭建自己的“微视”
  • 原文地址:https://www.cnblogs.com/muyuge/p/6152308.html
Copyright © 2011-2022 走看看