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

  • 相关阅读:
    bzoj 1176 cdq分治套树状数组
    Codeforces 669E cdq分治
    Codeforces 1101D 点分治
    Codeforces 1100E 拓扑排序
    Codeforces 1188D Make Equal DP
    Codeforces 1188A 构造
    Codeforces 1188B 式子转化
    Codeforces 1188C DP 鸽巢原理
    Codeforces 1179D 树形DP 斜率优化
    git commit -m "XX"报错 pre -commit hook failed (add --no-verify to bypass)问题
  • 原文地址:https://www.cnblogs.com/IntelligentBrain/p/5111300.html
Copyright © 2011-2022 走看看