天天看點

RaidoButton單選框

應用場景:實作單選效果    eg:問卷調查  性别選中   

xml:

RadioButton

 android:checked="true"      設定是否     選中 

 android:text="RadioButton" 文本

  注意:

RaidoButton要實作單選效果必須放在RadioGroup中

RadioGroup設定方向:

android:orientation="horizontal"  水準

code:

//獲得RadioGroup

RadioGroup mGroup=(RadioGroup) findViewById(R.id.radioGroup1);

//獲得被選中的RadioButtonId

int checkedRadioButtonId = mGroup.getCheckedRadioButtonId();

//查找選中的RadioButton

RadioButton button = (RadioButton) findViewById(checkedRadioButtonId);

//獲得選中的RadioButton的文本

String string = button.getText().toString();

//設定RadioButton狀态改變監聽事件

mGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override

            public void onCheckedChanged(RadioGroup group, int checkedId) {

                //獲得被選中的RadioButton

                RadioButton button = (RadioButton) findViewById(checkedId);

                //獲得選中的RadioButton的文本

                String string = button.getText().toString();

                mTv.setText(string);

            }