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

  • 相关阅读:
    delphi7调用webservice Java 传入参数为空
    delphi客户端调服务器端的java webservice如何在参数中传对象? 转
    DELPHI7如何调用带参数的JAVA WebService
    Delphi调用java开发的WebService,传入参数出错
    cxgrid按条件计算合计值 TcxTreeList计算合计值
    收款凭证
    delphi TCXTreelist 通过代码控制行的可编辑性
    win7 共享
    单到冲回的暂估方式,暂估入库的操作流程是怎样的?
    js常用函数和常用技巧
  • 原文地址:https://www.cnblogs.com/IntelligentBrain/p/5111300.html
Copyright © 2011-2022 走看看