Как получить значение от радиогруппы для другого действия?

Я хочу сделать новое действие, например, подтвердить, какой переключатель отмечен.

  • У меня есть 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>

Я удалил некоторые радио-кнопки, так что это не займет много времени.

Обратите внимание, я новичок.


person SmiLe ...    schedule 05.11.2012    source источник
comment
Это два отдельных вопроса. 1 — как получить индекс радио, а другой — как передать значение из одного действия в другое. Google отделить те два, что ваше решение вы найдете!   -  person Budius    schedule 05.11.2012
comment
10 RadioButton в строке... это перебор. Как насчет одного выбора ListView или Spinner ?   -  person S.D.    schedule 05.11.2012
comment
@wingman должно быть 2 варианта из списка, поэтому я не могу использовать список, но я не знаю о счетчике, могу ли я сделать 2 отдельных счетчика на одной странице   -  person SmiLe ...    schedule 05.11.2012
comment
@SmiLe... У вас может быть 2 ListView или 2 Spinner в одном макете.   -  person S.D.    schedule 06.11.2012


Ответы (2)


Сначала привяжите RadioGroup:

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

Затем получите положение кнопки, которая была проверена:

int checked = rg.getCheckedRadioButtonId();

Затем создайте намерение вашего следующего действия:

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

Затем поместите информацию, которую вы хотите передать, в намерение:

intent.putExtra("checked",checked);

Затем начните следующее действие:

startActivity(intent);

При следующем действии вы восстанавливаете эту информацию следующим образом:

Intent intent = getIntent();
int checked = intent.getIntExtra("checked");
person Daniel Scocco    schedule 05.11.2012

Попробуй это;

 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");
person KEYSAN    schedule 05.11.2012
comment
добавлена ​​кнопка OnClickListener. так? #Улыбка - person KEYSAN; 06.11.2012