Android - 使用 LinearLayout 检查 RadioGroup 中的 RadioButton ID

Android - Check RadioButton ID in a RadioGroup with LinearLayout(Android - 使用 LinearLayout 检查 RadioGroup 中的 RadioButton ID)
本文介绍了Android - 使用 LinearLayout 检查 RadioGroup 中的 RadioButton ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在这个布局中获得选中的单选按钮?因为 rg.getCheckedRadioButtonId() 不适用于此布局.我无法获得每个 radiobuttons ID.这就像我所有的 radio button 都在我的 radio Group

Is there any possible way to get the selected radio button in this layout? because rg.getCheckedRadioButtonId() not working on this layout. I can't get each of my radiobuttons ID. It's like all my radio button are out of my radio Group

  <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/rg"
        android:gravity="center"
        android:layout_centerVertical="true"
        android:layout_alignParentStart="true">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <RadioButton
                android:id="@+id/rad1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"/>

            <RadioButton
                android:id="@+id/rad2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="5dp"    />
        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <RadioButton
                android:id="@+id/rad3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"/>

            <RadioButton
                android:id="@+id/rad4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="5dp"/>
        </LinearLayout>
    </RadioGroup>

所以我所做的就是这样,但我只能得到一个radiobutton的ID.如何获取我的 radiobutton 的所有 ID 并获取选择的内容?

so what i did is like this, but I can only get the ID of one radiobutton. How can I get all ID of my radiobutton and get what is selected?.

                       bt.setOnClickListener(new View.OnClickListener() {
                                  @Override
                                  public void onClick(View v) {

                                      int radioButtonId = rg.getCheckedRadioButtonId();
                                      if(rg.getCheckedRadioButtonId()!= -1){

                                          RadioButton selectedans = (RadioButton) findViewById(radioButtonId);
                                          String selectedansText = selectedans.getText().toString();

                                          if (selectedansText == answer[position]) {

                                               //SelectedansText match with answer

                                          }
                                          if(selectedansText != answer[position]) {
                                                //selectedansText not match with answer
                                          }
                                          rg.clearCheck();

                                          if (position < question.length) {
                                              tv.setText(question[position]);
                                              r1.setText(opts[position * 4]);
                                              r2.setText(opts[position * 4 + 1]);
                                              r3.setText(opts[position * 4 + 2]);
                                              r4.setText(opts[position * 4 + 3]);
                                          } else {

                                              Intent intent = new Intent(grade_four_post_test.this, grade_four_post_results.class);
                                              startActivity(intent);

                                          }
                                      }
                                      else
                                      {
                                          Toast.makeText(grade_four_post_test.this, "Choose Answer",
                                                  Toast.LENGTH_SHORT).show();

                                      }
                                  }
                              }
        );

推荐答案

查看这个我在工作室做的答案,希望对你有帮助,

Check this answer i have done in my studio, hope this helps,

    public class StackOne extends AppCompatActivity {

    private RadioButton rb1, rb2, rb3, rb4;
    private RadioGroup rg;
    private String selectedType = "";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.stack_one);

        rb1 = (RadioButton) findViewById(R.id.rad1);
        rb2 = (RadioButton) findViewById(R.id.rad2);
        rb3 = (RadioButton) findViewById(R.id.rad3);
        rb4 = (RadioButton) findViewById(R.id.rad4);
        rg = (RadioGroup) findViewById(R.id.rg);

        GetSelectedRadioButton();
    }

    private void GetSelectedRadioButton() {

        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                switch (group.getCheckedRadioButtonId()) {
                    case R.id.rad1:
                        selectedType = "rButton1";
                        rb1.setChecked(true);
                        Toast.makeText(StackOne.this,"Radiobutton 1 checked",Toast.LENGTH_LONG).show();
                        break;
                    case R.id.rad2:
                        selectedType = "rButton2";
                        rb2.setChecked(true);
                        Toast.makeText(StackOne.this,"Radiobutton 2 checked",Toast.LENGTH_LONG).show();
                        break;
                    case R.id.rad3:
                        selectedType = "rButton2";
                        rb3.setChecked(true);
                        Toast.makeText(StackOne.this,"Radiobutton 3 checked",Toast.LENGTH_LONG).show();
                        break;
                    case R.id.rad4:
                        selectedType = "rButton2";
                        rb4.setChecked(true);
                        Toast.makeText(StackOne.this,"Radiobutton 4 checked",Toast.LENGTH_LONG).show();
                        break;

                }
            }
        });
    }
}

结帐此了解更多详情.

这篇关于Android - 使用 LinearLayout 检查 RadioGroup 中的 RadioButton ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)