Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android RadioGroup實現單選以及默認選中

android RadioGroup實現單選以及默認選中

編輯:關於Android編程

本文將通過radiogroup和radiobutton實現組內信息的單選,

其中radiogroup就是將radiobutton進行分組,同一管理和控制

同時實現默認選中情況,獲取默認值.效果圖

\


具體實例如下:<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+MS5hY3Rpdml0eV9tYWluLnhtbDwvcD4KPHA+PHByZSBjbGFzcz0="brush:java;">
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);
				}
			}
        });
    }


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved