Android 各控件的使用 - 单选框(RadioButton)

安卓单选框的使用

依然是Lesson项目,项目截图就不贴了。想知道的,看上一篇

依旧是添加个Activity,src文件夹下添加一个RadioButtonActivity

 

ExpandedBlockStart.gif View Code
package  cn.Kurodo;

import  android.app.Activity;
import  android.os.Bundle;
import  android.view.Gravity;
import  android.widget.Toast;
import  android.widget.RadioButton;
import  android.widget.RadioGroup;
import  android.widget.RadioGroup.OnCheckedChangeListener;

public   class  RadioButtonActivity  extends  Activity {
    
    
public  RadioButtonActivity(){
        
    }

    
private  RadioGroup m_radioGroup;
    
private  RadioButton    m_radioBtn;
    
    @Override
    
protected   void  onCreate(Bundle savedInstanceState) {
        
super .onCreate(savedInstanceState);
        setContentView(R.layout.radiobutton);
        
        m_radioGroup 
=  (RadioGroup)findViewById(R.id.selectGroup);
        m_radioBtn 
=  (RadioButton)findViewById(R.id.bothnot);
        
    
// 设置事件监听
    m_radioGroup.setOnCheckedChangeListener( new  RadioGroupListener());
    }
    
        
// 显示个浮动消息框
     protected   void  displayMessage(String msg)
    {
        Toast message 
=  Toast.makeText( this , msg, Toast.LENGTH_LONG);
        message.setGravity(Gravity.BOTTOM, 
0 200 );
        message.show();
    }
    
        
// 内部类
     class  RadioGroupListener  implements  OnCheckedChangeListener {

        @Override
        
public   void  onCheckedChanged(RadioGroup group,  int  checkedId) {
            
if (checkedId  ==  m_radioBtn.getId())
            {
                displayMessage(
" 正确答案: "   +  m_radioBtn.getText()  +   " ,恭喜你,回答正确! " );
            }
            
else
            {
                displayMessage(
" 回答错误! " );
            }
        }
    }
}

 

radiobutton的布局文件

 

ExpandedBlockStart.gif View Code
<? 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/radio_title"
        
/>
    
< RadioGroup  android:id ="@+id/selectGroup"
        android:layout_width
="wrap_content"
        android:layout_height
="wrap_content"
        android:orientation
="vertical" >
        
< RadioButton 
            
android:id ="@+id/chunge"
            android:layout_width
="fill_parent"
            android:layout_height
="wrap_content"
            android:text
="@string/chunge"
        
/>
        
< RadioButton 
            
android:id ="@+id/zengge"
            android:layout_width
="fill_parent"
            android:layout_height
="wrap_content"
            android:text
="@string/zengge"
        
/>
        
< RadioButton 
            
android:id ="@+id/bothnot"
            android:layout_width
="fill_parent"
            android:layout_height
="wrap_content"
            android:text
="@string/bothnot"
        
/>
    
</ RadioGroup >
</ LinearLayout >

 

依旧是修改启动Activity为这次的RadioButtonActivity,这里就不贴码了。

运行效果

转载于:https://www.cnblogs.com/Kurodo/archive/2011/08/02/2125631.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值