android RadioGroup实现单选以及默认选中

代码下载链接:http://download.csdn.net/detail/a123demi/7511835

本文将通过radiogroup和radiobutton实现组内信息的单选, 

其中radiogroup就是将radiobutton进行分组,同一管理和控制

同时实现默认选中情况,获取默认值.效果图

android RadioGroup实现单选以及默认选中_第1张图片


具体实例如下:

1.activity_main.xml



    

    

        

        
    


2.strings.xml




    RadioGroupDemo
    请选择你的性别:
    Settings
    
    



3.MainActivity.java

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        defaultStr = this.getResources().getString(R.string.please_selected);
        seletedTv = (TextView) this.findViewById(R.id.diplay_seleted_item_tv);
        sexRg = (RadioGroup) this.findViewById(R.id.sex_rg);
        manRb = (RadioButton) this.findViewById(R.id.man_rb);
        womanRb = (RadioButton) this.findViewById(R.id.woman_rb);
        
        manRb.setChecked(true);
        seletedTv.setText(defaultStr + manRb.getText().toString());
        
        sexRg.setOnCheckedChangeListener(new OnCheckedChangeListener(){

			@Override
			public void onCheckedChanged(RadioGroup rg, int checkedId) {
				// TODO Auto-generated method stub
				if(checkedId == manRb.getId()){
					seletedTv.setText(defaultStr + manRb.getText().toString());
				}else if(checkedId == womanRb.getId()){
					seletedTv.setText(defaultStr + womanRb.getText().toString());
				}else{
					seletedTv.setText(defaultStr);
				}
			}
        });
    }


你可能感兴趣的:(Android)