Android進階之RadioButton選中值的獲取

RadioButton選中值的獲取
  首先RadioButton是嵌套在RadioGroup中的,即一個RadioGoup中可以擁有多個RadioButton,但是一般至少是兩個。而在佈局文件中,RadioButton外部嵌套RadioGroup,因此在獲取RadioButton的選中值時應該先獲取當前RadioGroup,利用RadioGroup得到用戶已經選中的RadioButton,這樣就可以利用getText()方法獲取選中內容。
  
第一種方式:
   通過radioGroup.getCheckedRadioButtonId()來得到選中的RadioButton的ID,從而利用findviewbyid得到RadioButton進而獲取選中值
1、佈局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="wf.com.radiobuttondemo.MainActivity">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="性別是:"
        />
    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <RadioButton
            android:id="@+id/radioMan"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:text="男"
            />
        <RadioButton
            android:id="@+id/radioWonan"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:text="女"
            />
    </RadioGroup>
    <TextView
        android:id="@+id/txt_sex"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="性別是:"
        />
</LinearLayout>

2、MainActivity
public class MainActivity extends Activity {

    @BindView(R.id.radioMan)
    RadioButton radioMan;
    @BindView(R.id.radioWonan)
    RadioButton radioWonan;
    @BindView(R.id.radioGroup)
    RadioGroup radioGroup;
    @BindView(R.id.txt_sex)
    TextView txtSex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
//通過RadioGroup的setOnCheckedChangeListener()來監聽選中哪一個單選按鈕

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                selectRadioButton();
            }
        });
    }

    private void selectRadioButton() {
//通過radioGroup.getCheckedRadioButtonId()來得到選中的RadioButton的ID,從而得到RadioButton進而獲取選中值

        RadioButton rb = (RadioButton)MainActivity.this.findViewById(radioGroup.getCheckedRadioButtonId());
        txtSex.setText(rb.getText());
    }
}

  第二種方式:
    需要利用一下三個方法
    (1)radiogroup.getChildCount()   獲取radiogroup中子組件(radioButton)的數目
    (2)radiogroup.getChildAt()         根據索引獲取當前索引對應的radioButton
    (3)radiobutton.isChecked()        判斷當前組件是否被選中
    整體思路是,對radiogroup中組件進行循環,依次判斷isChecked(),從而找到選中的組件
1、佈局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="wf.com.radiobuttondemo.MainActivity">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="性別是:"
        />
    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <RadioButton
            android:id="@+id/radioMan"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:text="男"
            />
        <RadioButton
            android:id="@+id/radioWonan"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:text="女"
            />
    </RadioGroup>
    <TextView
        android:id="@+id/txt_sex"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="性別是:"
        />
    <Button
        android:id="@+id/btn_submit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="提交"

        />
</LinearLayout>

2、MainActivity
public class MainActivity extends Activity {
    @BindView(R.id.radioMan)
    RadioButton radioMan;
    @BindView(R.id.radioWonan)
    RadioButton radioWonan;
    @BindView(R.id.radioGroup)
    RadioGroup radioGroup;
    @BindView(R.id.txt_sex)
    TextView txtSex;
    @BindView(R.id.btn_submit)
    Button btnSubmit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

    }

    @OnClick(R.id.btn_submit)
    public void onClick() {
        for(int i = 0 ;i < radioGroup.getChildCount();i++){
            RadioButton rb = (RadioButton)radioGroup.getChildAt(i);
            if(rb.isChecked()){
                txtSex.setText(rb.getText());
                break;
            }
        }
    }
}



   



發佈了34 篇原創文章 · 獲贊 24 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章