美文网首页
Android 中获取RadioButton选中值的两种方式

Android 中获取RadioButton选中值的两种方式

作者: 追梦小乐 | 来源:发表于2018-07-26 11:06 被阅读472次

布局代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/style_match_background_content"
    android:orientation="vertical">

    <RadioGroup
        android:layout_marginTop="@dimen/dimen_50"
        android:id="@+id/rg_select"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >

        <android.support.v4.widget.Space
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content" />

        <RadioButton
            android:id="@+id/rb_problem"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="遇到问题"
            android:textColor="@color/c_black_1"
            android:textSize="26sp"
            />

        <RadioButton
            android:id="@+id/rb_suggestion"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="新功能建议"
            android:textColor="@color/c_black_1"
            android:textSize="26sp"
            />

        <android.support.v4.widget.Space
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content" />

    </RadioGroup>
</LinearLayout>

第一种方法:

   /**
     * 初始化监听
     */
    private void initListener(){
        btnConfirm.setOnClickListener(this);

        //RadioGroup选取值的方法一
        rgSelect.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                selectRadioButton();
            }
        });
    }

    /**
     * 获取RadioButton选中值
     */
    private void selectRadioButton() {
        RadioButton rb = (RadioButton)getActivity().findViewById(rgSelect.getCheckedRadioButtonId());
        CharSequence text = rb.getText();

    }

第二种方法:

 //RadioGroup选取值的方法二
            for(int i = 0 ;i < rgSelect.getChildCount();i++){
                RadioButton rb = (RadioButton)rgSelect.getChildAt(i);
                if(rb.isChecked()){
                    break;
                }
            }

完整代码:

public class UserFeedbackFragment extends BaseFragment {

    private static final String TAG = UserFeedbackFragment.class.getSimpleName();

    private RadioGroup rgSelect;
    private RadioButton rbProblem;
    private RadioButton rbSuggestion;
    private LinearLayout lltContentView;
    private EditText etTitle;
    private EditText etContactInfo;
    private EditText etContent;
    private Button btnConfirm;

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_user_feedbak;
    }

    @Override
    protected void initFragmentView() {
        rgSelect = xFindViewById(R.id.rg_select);
        rbProblem = xFindViewById(R.id.rb_problem);
        rbSuggestion = xFindViewById(R.id.rb_suggestion);
        btnConfirm = xFindViewById(R.id.btn_confirm);
    }

    @Override
    protected void initFragmentOperate() {

    }

    /**
     * 初始化监听
     */
    private void initListener(){
        btnConfirm.setOnClickListener(this);

        //RadioGroup选取值的方法一
        rgSelect.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                selectRadioButton();
            }
        });
    }

    /**
     * 获取RadioButton选中值
     */
    private void selectRadioButton() {
        RadioButton rb = (RadioButton)getActivity().findViewById(rgSelect.getCheckedRadioButtonId());
        CharSequence text = rb.getText();

    }


    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v.equals(btnConfirm)){

            //RadioGroup选取值的方法二
            for(int i = 0 ;i < rgSelect.getChildCount();i++){
                RadioButton rb = (RadioButton)rgSelect.getChildAt(i);
                if(rb.isChecked()){
                    break;
                }
            }

        }
    }
}

相关文章

网友评论

      本文标题:Android 中获取RadioButton选中值的两种方式

      本文链接:https://www.haomeiwen.com/subject/zmnvmftx.html