【问题标题】:How to get a value from radiogroup to another activity?如何从 radiogroup 获取值到另一个活动?
【发布时间】:2012-10-25 13:42:06
【问题描述】:

我想做一个新的活动,比如确认选中了哪个单选按钮。

  • 我有 2 个无线电组
  • 每组至少有 10 个单选按钮
  • 我想将选中的任何单选按钮的值带到下一个活动中

并且肯定只能从每个单选组中选择一个单选按钮

这是我的 xml 代码,我想要 java 代码

  <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="391dp"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Where Are You" />

            <RadioGroup
                android:id="@+id/radioGroup0"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:scrollbarStyle="insideOverlay"
                android:scrollbars="vertical" >

                <RadioButton
                    android:id="@+id/radio0"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="it" />

                <RadioButton
                    android:id="@+id/radio1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Eng" />

                <RadioButton
                    android:id="@+id/radio2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="there" />
                <RadioButton
                    android:id="@+id/radio3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="it" />

                <RadioButton
                    android:id="@+id/radio4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Eng" />

                <RadioButton
                    android:id="@+id/radio5"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="there" />
                <RadioButton
                    android:id="@+id/radio6"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="it" />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Where Do You Want To Go" />

            <RadioGroup
                android:id="@+id/radioGroup1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:scrollbarStyle="insideOverlay"
                android:scrollbars="vertical" >

                <RadioButton
                    android:id="@+id/radio12"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="it" />

                <RadioButton
                    android:id="@+id/radio13"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Eng" />

                <RadioButton
                    android:id="@+id/radio14"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="there" />
                <RadioButton
                    android:id="@+id/radio15"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="it" />

                <RadioButton
                    android:id="@+id/radio16"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Eng" />

                <RadioButton
                    android:id="@+id/radio17"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="there" />

            </RadioGroup>

            <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="OK" />

        </LinearLayout>
    </ScrollView>

我已经删除了一些单选按钮,所以它不会太长

请注意我是初学者。

【问题讨论】:

  • 这是两个不同的问题。一是如何获取无线电索引,二是如何将值从一个活动传递到另一个活动。谷歌将这两者分开,您会找到您的解决方案!
  • 10 RadioButton 排成一行……这太过分了。单选ListViewSpinner怎么样?
  • @wingman 列表中必须有 2 个选项,所以我不能使用 listview 但我不知道微调器我可以在同一页面中制作 2 个单独的微调器
  • @SmiLe... 您可以在同一布局中拥有 2 个 ListView 或 2 个 Spinner

标签: android android-activity radio-button radio-group


【解决方案1】:

首先,绑定 RadioGroup:

RadioGroup rg = (RadioGroup) findViewById(R.id.radioGroup1);

然后获取被选中的按钮的位置:

int checked = rg.getCheckedRadioButtonId();

然后创建下一个活动的意图:

Intent intent = new Intent(this,nextActivity.class);

然后把你要传递的信息放在intent上:

intent.putExtra("checked",checked);

然后开始下一个活动:

startActivity(intent);

在下一个活动中,您可以像这样恢复该信息:

Intent intent = getIntent();
int checked = intent.getIntExtra("checked");

【讨论】:

    【解决方案2】:

    试试看;

     public void onCreate(Bundle savedInstanceState) {
                enter code here
                RadioGroup rg = (RadioGroup) findViewById(R.id.radioGroup1);
                String selectedRadioValue = ((RadioButton)findViewById(rg.getCheckedRadioButtonId() )).getText().toString();
                RadioGroup rg2 = (RadioGroup) findViewById(R.id.radioGroup2);
                String selectedRadioValue2 =((RadioButton)findViewById(rg.getCheckedRadioButtonId() )).getText().toString();
    
    
    
    
               Button btn = (Button) findViewById(R.id.buttonName);
               btn.setOnClickListener(new OnClickListener() {
    
                      @Override
                      public void onClick(View v) {
                      //enter code here for your control and
                          Intent intent = new Intent(getApplicationContext(), your.class);
                           intent.putExtra("radioGroup1Selected", selectedRadioValue);
                           intent.putExtra("radioGroup2Selected", selectedRadioValue2);
                          startActivity(intent);
                      }
                 });
    
    
    
       }
    

    获得另一个活动:

    Intent intent = getIntent();
    String selectedRadioValue = intent.getStringExtra("selectedRadioValue");
    String selectedRadioValue2 = intent.getStringExtra("selectedRadioValue2");
    

    【讨论】:

    • 添加了按钮 OnClickListener。像这样? #微笑
    猜你喜欢
    • 2019-08-18
    • 2015-02-16
    • 2017-10-28
    • 1970-01-01
    • 2018-11-17
    • 1970-01-01
    • 2020-11-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多