Android的 - 检查单选ID在一个的LinearLayout RadioGroup中单选、Android、ID、RadioGroup

由网友(揽一身可爱)分享简介:是否有任何可能的方式来获得所选择的单选按钮在此布局?因为 rg.getCheckedRadioButtonId()不工作的此布局。我不能让我的每个单选按钮ID 的。这就像我所有的单选按钮超出我的无线电集团 < RadioGroup中        机器人:layout_width =match_parent...

是否有任何可能的方式来获得所选择的单选按钮在此布局?因为 rg.getCheckedRadioButtonId()不工作的此布局。我不能让我的每个单选按钮ID 的。这就像我所有的单选按钮超出我的无线电集团

 < RadioGroup中        机器人:layout_width =match_parent        机器人:layout_height =WRAP_CONTENT        机器人:ID =@ + ID / RG        机器人:比重=中心        机器人:layout_centerVertical =真        机器人:layout_alignParentStart =真正的>        <的LinearLayout            机器人:layout_width =WRAP_CONTENT            机器人:layout_height =WRAP_CONTENT>            <单选                机器人:ID =@ + ID / RAD1                机器人:layout_width =WRAP_CONTENT                机器人:layout_height =WRAP_CONTENT                机器人:layout_marginLeft =5DP/>            <单选                机器人:ID =@ + ID / RAD2                机器人:layout_width =WRAP_CONTENT                机器人:layout_height =WRAP_CONTENT                机器人:layout_marginRight =5DP/>        < / LinearLayout中>        <的LinearLayout            机器人:layout_width =WRAP_CONTENT            机器人:layout_height =WRAP_CONTENT>            <单选                机器人:ID =@ + ID / RAD3                机器人:layout_width =WRAP_CONTENT                机器人:layout_height =WRAP_CONTENT                机器人:layout_marginLeft =5DP/>            <单选                机器人:ID =@ + ID / RAD4                机器人:layout_width =WRAP_CONTENT                机器人:layout_height =WRAP_CONTENT                机器人:layout_marginRight =5DP/>        < / LinearLayout中>    < / RadioGroup中> 

所以我所做的就是这样的,但我只能得到一个的ID 单选。我怎样才能让我的单选键,得到的是选择的所有ID?

  bt.setOnClickListener(新View.OnClickListener(){                                  @覆盖                                  公共无效的onClick(视图v){                                      INT radioButtonId = rg.getCheckedRadioButtonId();                                      如果(rg.getCheckedRadioButtonId()!=  -  1){                                          单选selectedans =(单选)findViewById(radioButtonId);                                          。字符串selectedansText = selectedans.getText()的toString();                                          如果(selectedansText ==答案[位置]){                                               // SelectedansText匹配的答案                                          }                                          如果(selectedansText!=答案[位置]){                                                // selectedansText不匹配的答案                                          }                                          rg.clearCheck();                                          如果(位置< question.length){                                              tv.setText(的问题[位置]);                                              r1.setText(选[位置* 4]);                                              r2.setText(选[位置* 4 + 1]);                                              r3.setText(选[位置* 4 + 2]);                                              r4.setText(选[位置* 4 + 3]);                                          }其他{                                              意向意图=新意图(grade_four_post_test.this,grade_four_post_results.class);                                              startActivity(意向);                                          }                                      }                                      其他                                      {                                          Toast.makeText(grade_four_post_test.this,选择答案,                                                  Toast.LENGTH_SHORT).show();                                      }                                  }                              }        ); 

解决方案 在Android中查看和管理sqlite数据库

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

 公共类StackOne扩展AppCompatActivity {    私人单选按钮RB1,RB2,RB3,RB4;    私人RadioGroup中RG;    私人字符串selectedType =;    @覆盖    公共无效的onCreate(捆绑savedInstanceState){        super.onCreate(savedInstanceState);        的setContentView(R.layout.stack_one);        RB1 =(单选)findViewById(R.id.rad1);        RB2 =(单选)findViewById(R.id.rad2);        RB3 =(单选)findViewById(R.id.rad3);        RB4 =(单选)findViewById(R.id.rad4);        RG =(RadioGroup中)findViewById(R.id.rg);        GetSelectedRadioButton();    }    私人无效GetSelectedRadioButton(){        rg.setOnCheckedChangeListener(新RadioGroup.OnCheckedChangeListener(){            @覆盖            公共无效onCheckedChanged(RadioGroup中组,诠释checkedId){                开关(group.getCheckedRadioButtonId()){                    案例R.id.rad1:                        selectedType =rButton1;                        rb1.setChecked(真);                        Toast.makeText(StackOne.this,单选框选中1,Toast.LENGTH_LONG).show();                        打破;                    案例R.id.rad2:                        selectedType =rButton2;                        rb2.setChecked(真);                        Toast.makeText(StackOne.this,单选框选中2,Toast.LENGTH_LONG).show();                        打破;                    案例R.id.rad3:                        selectedType =rButton2;                        rb3.setChecked(真);                        Toast.makeText(StackOne.this,单选框选中3,Toast.LENGTH_LONG).show();                        打破;                    案例R.id.rad4:                        selectedType =rButton2;                        rb4.setChecked(真);                        Toast.makeText(StackOne.this,单选框选中4,Toast.LENGTH_LONG).show();                        打破;                }            }        });    }} 

了解更多详情。

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>

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;

                }
            }
        });
    }
}

Checkout this for more detail.

阅读全文

相关推荐

最新文章