Android RadioGroup supports single-choice and default Selection

Source: Internet
Author: User

This article uses radiogroup and radiobutton to implement Single-Choice of intra-group information,

Radiogroup groups radiobutton for the same management and control.

At the same time, the default value is also obtained.



The specific example is as follows: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + MS5hY3Rpdml0eV9tYWluLnhtbDwvcD4KPHA + PHByZSBjbGFzcz0 = "brush: java;">
2. strings. xml

 
     
  
   
RadioGroupDemo
      
  
   
Select your gender:
      
  
   
Settings
      
  
   
Male
      
  
   
Female
  
 

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(){@Overridepublic void onCheckedChanged(RadioGroup rg, int checkedId) {// TODO Auto-generated method stubif(checkedId == manRb.getId()){seletedTv.setText(defaultStr + manRb.getText().toString());}else if(checkedId == womanRb.getId()){seletedTv.setText(defaultStr + womanRb.getText().toString());}else{seletedTv.setText(defaultStr);}}        });    }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.