Android getCheckedRadioButtonId() 返回无用的整数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10356733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:25:21  来源:igfitidea点击:

getCheckedRadioButtonId() returning useless int?

androidandroid-layoutonclick

提问by Davek804

I have a button's onClickListener that needs to detect which radiobutton was selected when the user clicks the button. Currently, the Log.v you see below in the onClickListener is not returning a useless bit of info:

我有一个按钮的 onClickListener 需要检测用户单击按钮时选择了哪个单选按钮。目前,您在 onClickListener 下面看到的 Log.v 没有返回一些无用的信息:

This is clicking submit three times with a different radio selected each time:

这是点击提交三次,每次选择不同的收音机:

04-27 19:24:42.417: V/submit(1564): 1094168584

04-27 19:24:45.048: V/submit(1564): 1094167752

04-27 19:24:47.348: V/submit(1564): 1094211304

04-27 19:24:42.417:V/提交(1564):1094168584

04-27 19:24:45.048:V/提交(1564):1094167752

04-27 19:24:47.348:V/提交(1564):1094211304

So, I need to know which radioButton is actually selected - is there a way to get the object of the radiobutton? I want to be able to get it's id# from XML, as well as its current text.

所以,我需要知道实际选择了哪个单选按钮 - 有没有办法获得单选按钮的对象?我希望能够从 XML 中获取它的 id# 以及它的当前文本。

Here's the relevant code:

这是相关的代码:

public void buildQuestions(JSONObject question) throws JSONException {

    radioGroup = (RadioGroup) questionBox.findViewById(R.id.responseRadioGroup);

    Button chartsButton = (Button) questionBox.findViewById(R.id.chartsButton);
    chartsButton.setTag(question);
    Button submitButton = (Button) questionBox.findViewById(R.id.submitButton);

    chartsButton.setOnClickListener(chartsListener);
    submitButton.setOnClickListener(submitListener);

    TagObj tagObj = new TagObj(question, radioGroup);
    submitButton.setTag(tagObj);

}

public OnClickListener submitListener = new OnClickListener() {
    public void onClick(View v) {
        userFunctions = new UserFunctions();
        if (userFunctions.isUserLoggedIn(activity)) {
            TagObj tagObject = (TagObj) v.getTag();
            RadioGroup radioGroup = tagObject.getRadioGroup();
            JSONObject question = tagObject.getQuestion();

            Log.v("submit", Integer.toString(radioGroup.getCheckedRadioButtonId()));
            SubmitTask submitTask = new SubmitTask((Polling) activity, question);
            submitTask.execute();

        }
    }   
};

回答by Luksprog

getCheckedRadioButtonId()returns the idof the RadioButton(or -1if no RadioButtonsare checked) that is checked in the Radiogroup. If you set distinct ids to the RadioButonsin the layout then you will try to match those ids with the return of the method to see which one is checked:

getCheckedRadioButtonId()返回idRadioButton(或-1如果没有RadioButtons被选中),其在检查Radiogroup。如果您RadioButons在布局中为设置了不同的 id,那么您将尝试将这些 id 与方法的返回值进行匹配,以查看检查了哪一个:

//field in the class
private static final int RB1_ID = 1000;//first radio button id
private static final int RB2_ID = 1001;//second radio button id
private static final int RB3_ID = 1002;//third radio button id

//create the RadioButton
RadioButton rb1 = new RadioButton(this);
//set an id
rb1.setId(RB1_ID);


    int btn = radioGroup.getCheckedRadioButtonId();
    switch (btn) {
    case RB1_ID:
        // the first RadioButton is checked.
    break;
        //other checks for the other RadioButtons ids from the RadioGroup
    case -1:
        // no RadioButton is checked inthe Radiogroup
    break;
    }

回答by Drake Clarris

store the checked ID, then compare it to each button using the function radioButton.getID() using a switch statement or if-else chains

存储已检查的 ID,然后使用函数 radioButton.getID() 使用 switch 语句或 if-else 链将其与每个按钮进行比较

回答by Levon Petrosyan

I am setting initial checked radioButton in RadioGroupxml

我正在RadioGroupxml 中设置初始选中的单选按钮

 <RadioGroup
          android:id="@+id/rgCustomized"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginStart="32dp"
          android:layout_marginTop="8dp"
          android:checkedButton="@+id/rbNotCustomized"
          android:orientation="horizontal">

          <RadioButton
              android:id="@+id/rbCustomized"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_marginEnd="50dp"
              android:text="@string/yes" />

          <RadioButton
              android:id="@+id/rbNotCustomized"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="@string/no" />

 </RadioGroup>

And I identify which radioButton is selected like this

我确定像这样选择了哪个单选按钮

rgCustomized.checkedRadioButtonId==rbCustomized.id

回答by MohammadL

I think relying on what radioGroup.getCheckedRadioButtonId()returns is not good practice if you want to store it into the database or to use it.

我认为,radioGroup.getCheckedRadioButtonId()如果您想将其存储到数据库中或使用它,那么依赖返回的内容并不是一个好习惯。

Because:

因为:

  • getCheckedRadioButtonId()value will keep changing for each RadioButtonand if there are two similar values (two views in the same hierarchy) Android will choose the first one. Unless you provided a unique Ids with method generateViewId()and set it to the view with setId().
  • getCheckedRadioButtonId()will return unknown value.
  • getCheckedRadioButtonId()每个值都会不断变化RadioButton,如果有两个相似的值(同一层次结构中的两个视图),Android 将选择第一个。除非您提供了一个唯一的 Id 方法generateViewId()并将其设置为带有setId().
  • getCheckedRadioButtonId()将返回未知值。

Therefore

所以

Switching on radioGroup.getCheckedRadioButtonId()and implement your custom values to each selection then use that custom value, not the View Id.

打开radioGroup.getCheckedRadioButtonId()并为每个选择实施自定义值,然后使用该自定义值,而不是视图 ID。

Example to use values from selected Radio Button:

使用选定单选按钮中的值的示例:

int selected = -1;
switch (radioGroup.getCheckedRadioButtonId()) {
    case R.id.one_radioButton:
        selected = 0;
        break;
    case R.id.two_radioButton:
        selected = 1;
        break;
    case R.id.three_radioButton:
        selected = 2;
        break;
}
    // return a custom value you specific
    Log.d(TAG, "selectedBox: " + selectedBox);   

    // return a random unknown number value
    Log.d(TAG, "radioGroup.getCheckedRadioButtonId(): " + radioGroup.getCheckedRadioButtonId());

Example to populate selected RadioButton to UI:

将选定的 RadioButton 填充到 UI 的示例:

    switch (selected) {
        case 0:
            oneRadioButton.setChecked(true);
            break;
        case 1:
            twoRadioButton.setChecked(true);
            break;
        case 2:
            threeRadioButton.setChecked(true);
            break;
    }