как получить идентификатор проверенного радио из группы радио

я создал динамическую радиогруппу, в которой каждая группа имеет три радиокнопки, поэтому при проверенном слушателе смены, как я могу получить идентификатор радиокнопки, проверенной в радиогруппе..... заранее спасибо..

    public class Rate_me_up extends Activity implements OnClickListener,
        OnCheckedChangeListener {
    LinearLayout layout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rate_me_up);
        layout = (LinearLayout) findViewById(R.id.linear);
        Button btnp = (Button) findViewById(R.id.buttonp);
        btnp.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        RadioGroup radioGroup = new RadioGroup(Rate_me_up.this);
        radioGroup.setOrientation(1);

        LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        layout.addView(radioGroup, p);
        RadioButton radioButtonView = new RadioButton(Rate_me_up.this);
        radioButtonView.setText("RadioButton");
        radioButtonView.setChecked(false);
        radioGroup.addView(radioButtonView, p);

        RadioButton radioButtonView2 = new RadioButton(Rate_me_up.this);
        radioButtonView2.setText("RadioButton2");
        radioButtonView2.setChecked(false);
        radioGroup.addView(radioButtonView2, p);
        RadioButton radioButtonView3 = new RadioButton(Rate_me_up.this);
        radioButtonView3.setText("RadioButton2");
        radioGroup.addView(radioButtonView3, p);
        radioButtonView3.setChecked(false);
        radioGroup.setOnCheckedChangeListener(this);

    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {

    }

}

person bhavdip    schedule 29.01.2014    source источник
comment
первый установленный идентификатор метода с RadioButton по setId().   -  person Spring Breaker    schedule 29.01.2014
comment
после этого... как получить id...   -  person bhavdip    schedule 29.01.2014
comment
checkedId в onCheckedChanged вернет вам идентификатор проверенного RadioButton.   -  person Apoorv    schedule 29.01.2014
comment
Если вы хотите получить идентификатор радио, нажмите кнопку или нажмите радио.   -  person Pratik Butani    schedule 29.01.2014
comment
Я разместил простой ответ stackoverflow.com/a/21422976/1318946, я думаю, вам нужно искать, это основной вопрос как bit.ly/1goR2HD   -  person Pratik Butani    schedule 29.01.2014


Ответы (5)


Преобразование значения радиокнопки в строку.

Вот так надо постараться

В onClick:

genradiocheck();
radiovalue =  radiobutton1.getText().toString();

`

    private boolean genradiocheck()
  {
    boolean gender_flag = false;

    if (r_group.getCheckedRadioButtonId() == -1) 
    {
      //
    } 
    else 
    {
        radiobutton1 = (RadioButton) findViewById(r_group
                  .getCheckedRadioButtonId());

     gender_flag = true;
    }

    return gender_flag;
  }

Надеюсь это поможет.

person MetaldroiD    schedule 29.01.2014

Это поможет вам:

int index = myRadioGroup.indexOfChild(findViewById(myRadioGroup.getCheckedRadioButtonId()));
person Pratik Butani    schedule 29.01.2014

Сначала установите идентификатор от RadioButton до setId(). Сделайте что-то вроде следующего:

@Override
        public void onClick(View v) {
            RadioGroup radioGroup = new RadioGroup(Rate_me_up.this);
            radioGroup.setOrientation(1);

            LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            layout.addView(radioGroup, p);
            RadioButton radioButtonView = new RadioButton(Rate_me_up.this);
            radioButtonView.setId(1);
            radioButtonView.setText("RadioButton");
            radioButtonView.setChecked(false);
            radioGroup.addView(radioButtonView, p);

            RadioButton radioButtonView2 = new RadioButton(Rate_me_up.this);
            radioButtonView.setId(2);
            radioButtonView2.setText("RadioButton2");
            radioButtonView2.setChecked(false);
            radioGroup.addView(radioButtonView2, p);

            RadioButton radioButtonView3 = new RadioButton(Rate_me_up.this);
            radioButtonView.setId(3);
            radioButtonView3.setText("RadioButton2");
            radioGroup.addView(radioButtonView3, p);
            radioButtonView3.setChecked(false);
            radioGroup.setOnCheckedChangeListener(this);

        }

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {

        switch (checkedId) {
                case 1:
                    // first radiobutton is clicked
                    break;
                case 2:
                    // second radiobutton is clicked
                    break;
                case 3:
                    // third radiobutton is clicked
                    break;


                default:
                    break;
        }
person Spring Breaker    schedule 29.01.2014

Вам нужно установить идентификатор, прежде чем пытаться получить идентификатор. Используйте следующий фрагмент кода.

   @Override
public void onClick(View v) {
    RadioGroup radioGroup = new RadioGroup(Rate_me_up.this);
    radioGroup.setOrientation(1);

    LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT);
    layout.addView(radioGroup, p);
    RadioButton radioButtonView = new RadioButton(Rate_me_up.this);
    radioButtonView.setId(1);
    radioButtonView.setText("RadioButton");
    radioButtonView.setChecked(false);
    radioGroup.addView(radioButtonView, p);

    RadioButton radioButtonView2 = new RadioButton(Rate_me_up.this);
    radioButtonView.setId(2);
    radioButtonView2.setText("RadioButton2");
    radioButtonView2.setChecked(false);
    radioGroup.addView(radioButtonView2, p);
    RadioButton radioButtonView3 = new RadioButton(Rate_me_up.this);
    radioButtonView.setId(3);
    radioButtonView3.setText("RadioButton2");
    radioButtonView3.setChecked(false);
    radioGroup.addView(radioButtonView3, p);
    radioGroup.setOnCheckedChangeListener(this);

}

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
Toast.makeText(Rate_me_up.this, "id : "+checkedId, Toast.LENGTH_SHORT).show();//or you can also get the id by using group.getCheckedRadioButtonId()
}
person Mehul Joisar    schedule 29.01.2014

Поле checkId в

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
}

даст вам проверенный идентификатор RadioButton.

редактировать:

сделай это так:

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {

switch(checkedId){
   case R.id.radio1: //assumed id of radioButton1
   //statements
   break;

   case R.id.radio2:
   .
   break; //so on
 }
}

для динамических radioButtons сравнивайте путем получения идентификатора оттуда, например, radioButton1.getId();

person Ana    schedule 29.01.2014
comment
Он уже установил setOnCheckedChangeListener в последней строке onClick. - person Apoorv; 29.01.2014
comment
поясните, пожалуйста, что именно вы имеете в виду. я имею в виду, знаете ли вы, сколько радиокнопок будет сгенерировано, или это полностью зависит от действий пользователя во время выполнения? - person Ana; 29.01.2014