(初识android) android组件 RadioButton,CheckBox,Toast简单介绍

        RadioButton单选框,CheakBox多选框,Toast翻译过来就是:吐司,烤面包片[U],在android肯定不是这个意思啦,它有点像是一个对话框Dialog,但是又不同,它会在显示后过一段时间自动消失,给用户已提示作用。

 

 1、RadioButton,CheakBox在布局中:    

 <RadioGroup 
        android:id="@+id/rg"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >
      
        <RadioButton 
            android:id="@+id/manrb"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:text="@string/manrb"
            android:checked="true"
              />
         <RadioButton 
            android:id="@+id/womanrb"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:text="@string/womanrb"
          
            />
      </RadioGroup>
     
       
     <CheckBox 
         android:id="@+id/swimckb"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:text="@string/swimckb"
         android:layout_gravity="right"
         />
     <CheckBox 
         android:id="@+id/readckb"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:text="@string/readckb"
        />
     <CheckBox 
         android:id="@+id/runckb"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:text="@string/swimckb"
         />

 从上我们可以看出,他们的大体使用和java并没有什么特别的变化,在使用RadioButton是必须是在一个RadioGroup中。

 2.初始化

//初始化单选框
        rg=(RadioGroup)findViewById(R.id.rg);
        manrb=(RadioButton)findViewById(R.id.manrb);
        womanrb=(RadioButton)findViewById(R.id.womanrb);
        
        //初始化多选框
        swimckb=(CheckBox)findViewById(R.id.swimckb);
        readckb=(CheckBox)findViewById(R.id.readckb);
        runckb=(CheckBox)findViewById(R.id.runckb);


 3.在相应的类中首先进行初始化和其他安卓控件一样,但在添加监听事件时有所不同,在RadioGroup添加监听是实现RadioGroup 的OnCheckedChangeListener事件,而相对应的CheckBox没有相对应的OnCheckedChangeListener,是使用CompoundButton的OnCheckedChangeListener事件。 给单选框添加监听事件时,是对RadioGrup进行监听,然后就、根据选择(CheckId和你的RadioButton)相匹配,在执行相应的操作   

  //给单选框添加监听事件
        rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				if(manrb.getId()==checkedId)
				{
					toast=Toast.makeText(RadioButtonActivity.this,"您选择的是男", Toast.LENGTH_SHORT);
					toast.setGravity(Gravity.CENTER, 50,-100);
					toast.show();
					
				}
				else if(womanrb.getId()==checkedId)
				{
					toast=Toast.makeText(RadioButtonActivity.this,"您选择的是女", Toast.LENGTH_SHORT);
					toast.setGravity(Gravity.CENTER, 50,-100);
					toast.show();
					
				}
			}
		});
        
        //给多选框添加监听
        swimckb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			
        	@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				if(isChecked)
				{
				 //执行操作
				}
				else{
				//执行操作
				}
			}
		});
        
        readckb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			
        	@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				if(isChecked)
				{
				 //执行操作
				}
				else{
				//执行操作
				}
			}
		});
 
        runckb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			
        	@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				if(isChecked)
				{
				 //执行操作
				}
				else{
				//执行操作
				}
			}
		});


 

4.Toast的使用,它并不需要在布局文件进行布局,直接实例化一个Toast直接可使用。 具体使用:

toast=Toast.makeText(RadioButtonActivity.this,"您选择的是女", Toast.LENGTH_SHORT);
					toast.setGravity(Gravity.CENTER, 50,-100);
					toast.show();

 

 Toast具体说明:http://www.cnblogs.com/GnagWang/archive/2010/11/26/1888762.html

           

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值