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差不多,但是它没有组的概念,这里就不介绍了,大家举一反三.

  • 相关阅读:
    Vector(同步)和ArrayList(异步)异同
    集合框架(1)
    如何优化limit
    Mysql5大引擎之间的区别和优劣之分
    差分约束 poj 3169
    最大权森林 poj 3723
    次短路 poj 3255
    P1604 B进制星球 (高精度进制计算器)
    最小生成树入门 kruskal和堆优化的prim
    并查集入门 POJ 1182(带权并查集)
  • 原文地址:https://www.cnblogs.com/IntelligentBrain/p/5111300.html
Copyright © 2011-2022 走看看