1

我已經創建了動態收音機組,其中每個組都有三個單選按鈕,所以選中的更改監聽器如何獲得收音機組中選中的單選按鈕的ID .....謝謝提前..如何獲得收音機組的選中的收音機ID

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) { 

    } 

} 
+0

首先通過setId()方法設置'RadioButton'的id。 –

+0

這樣做之後...如何獲得ID ... – bhavdip

+0

'onCheckedChanged'中的'checkedId'會返回您選中的'RadioButton'的ID。 – Apoorv

回答

-1

領域checkId在

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

會給你檢查單選按鈕的ID。

編輯:

做這樣的:

@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 
} 
} 

動態單選按鈕比較通過獲取ID從那裏insatnce 像radioButton1.getId();

+0

他已經在'onClick'的最後一行設置了'setOnCheckedChangeListener'。 – Apoorv

+0

請解釋你的意思。我的意思是你知道有多少個單選按鈕會被生成,或者完全取決於用戶在運行時的操作? –

0
switch (radioXO.getCheckedRadioButtonId()) { 
      case R.id.rdo_X: 
       player1Character = "X"; 
       break; 
      case R.id.rdo_O: 
       player1Character = "O"; 
       break; 

      } 
0

這將幫助您:

int index = myRadioGroup.indexOfChild(findViewById(myRadioGroup.getCheckedRadioButtonId())); 
0

首先通過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; 
     } 
1

轉換單選框值轉換成字符串設定RadioButton的ID。

這就是你需要嘗試,

上的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; 
    } 

希望這有助於。

0

您需要在嘗試獲取ID之前設置該ID。使用下面的一段代碼。

@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() 
}