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

时间:2016-01-29 13:03:37

标签: android radio-button

是否有可能在此布局中获取所选的radio button?因为rg.getCheckedRadioButtonId()无法处理此布局。我不能得到我的radiobuttons ID。这就像我radio button

中的所有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并获取所选内容?

                       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();

                                      }
                                  }
                              }
        );

2 个答案:

答案 0 :(得分:1)

检查我在我的工作室完成的这个答案,希望这有帮助,

    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;

                }
            }
        });
    }
}

结帐this了解更多详情。

答案 1 :(得分:0)

您可以通过这种方式获得选定的单选按钮

int selectedId = radioGroup.getCheckedRadioButtonId();
// find the radiobutton by returned id
radioButton = (RadioButton) findViewById(selectedId);

这将返回所选单选按钮的ID