• 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>


  • 相关阅读:
    几个数之和----数组刷题
    单调栈刷题
    腾讯金融科技凉经
    mysql刷题
    链表类题目常用方法
    阿里云一面凉经
    腾讯TEG一面凉经
    腾讯软件开发-后台开发实习生-一面凉经
    剑指 Offer 19. 正则表达式匹配
    剑指 Offer 20. 表示数值的字符串
  • 原文地址:https://www.cnblogs.com/muyuge/p/6152308.html
Copyright © 2020-2023  润新知