zoukankan      html  css  js  c++  java
  • android教程之用户界面---单选按钮RadioButton||多选按钮CheckBox

            Android提供了一个圆形的单选按钮RadioButton,通过它,用户可以进行单选操作.二单选的集合则放在RadioGroup中,在这个集合中,用户只能选中一个RadioButton,而需要注意的是,监听事件要绑定在RadioGroup上,要不然,监听会有意想不到的结果.

    radiobutton.xml   [ 其中的@string/....内容大家可以自己填写]

     <TextView 
            android:id="@+id/show"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/qt"/>
    	        
        <RadioGroup 
            android:id="@+id/rg"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/show"
            android:orientation="vertical">
    		<RadioButton 
    		    android:id="@+id/rb1"
    		    android:layout_width="match_parent"
    		    android:layout_height="wrap_content"
    		    android:text="@string/bt1"/>	
    		<RadioButton 
    		    android:id="@+id/rb2"
    		    android:layout_width="match_parent"
    		    android:layout_height="wrap_content"
    		    android:text="@string/bt2"/>
    		<RadioButton 
    		    android:id="@+id/rb3"
    		    android:layout_width="match_parent"
    		    android:layout_height="wrap_content"
    		    android:text="@string/bt3"/>
    		<RadioButton 
    		    android:id="@+id/rb4"
    		    android:layout_width="match_parent"
    		    android:layout_height="wrap_content"
    		    android:text="@string/bt4"/>
            
        </RadioGroup>
        <TextView 
            android:id="@+id/answer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/rg"/>

    TestRadioGroup.java

    public class TestRadioGroup extends Activity{
    	private RadioButton rb1,rb2,rb3,rb4;
    	private RadioGroup rg;
    	private TextView tv;
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		// TODO Auto-generated method stub
    		super.onCreate(savedInstanceState);
    		this.setContentView(R.layout.radiobutton);
    		rb1=(RadioButton) this.findViewById(R.id.rb1);
    		rb2=(RadioButton) this.findViewById(R.id.rb2);
    		rb3=(RadioButton) this.findViewById(R.id.rb3);
    		rb4=(RadioButton) this.findViewById(R.id.rb4);
    		
    		rg=(RadioGroup) this.findViewById(R.id.rg);
    		
    		tv=(TextView) this.findViewById(R.id.answer);
    		
    		rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    			
    			@Override
    			public void onCheckedChanged(RadioGroup group, int checkedId) {
    				// TODO Auto-generated method stub
    				if(checkedId==rb1.getId()){
    					tv.setText("恭喜你,答对了!");
    				}else{
    					tv.setText("正确答案是:A");
    				}
    			}
    		});
    	}
    }

    CheckBox的使用方法更RadioButton差不多,但是它没有组的概念,这里就不介绍了,大家举一反三.

  • 相关阅读:
    IE8发送ajax请求无效
    Qt 桌面服务 QDesktopServices
    Qt 排序 QSort
    使用ehcache缓存可变对象时的注意事项
    Golang微服务入门到精通之路-1-Go之初体验
    机械革命 Code01 笔记本激活 Win10 方法
    IM技术分享:万人群聊消息投递方案的思考和实践
    零基础入门:基于开源WebRTC,从0到1实现实时音视频聊天功能
    剑指 Offer 28. 对称的二叉树
    idea使用
  • 原文地址:https://www.cnblogs.com/IntelligentBrain/p/5111300.html
Copyright © 2011-2022 走看看