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

  • 相关阅读:
    iOS中Zbar二维码扫描的使用
    SOJ 1135. 飞跃原野
    SOJ 1048.Inverso
    SOJ 1219. 新红黑树
    SOJ 1171. The Game of Efil
    SOJ 1180. Pasting Strings
    1215. 脱离地牢
    1317. Sudoku
    SOJ 1119. Factstone Benchmark
    soj 1099. Packing Passengers
  • 原文地址:https://www.cnblogs.com/IntelligentBrain/p/5111300.html
Copyright © 2011-2022 走看看