2017-06-20 35 views
0

如何在單擊按鈕時從android中的Radio組中選擇多個單選按鈕值的數據..並顯示全部數據..代碼正在工作,但堅持..當一個按鈕提交併沒有獲取數據。如何在android中單擊按鈕時從android收音機組中獲取所選多個單選按鈕值的數據

這裏是代碼:

import android.os.Bundle; 
import android.app.Activity; 
import android.util.Log; 
import android.view.Menu; 
import android.view.View; 
import android.widget.Button; 
import android.widget.LinearLayout; 
import android.widget.RadioButton; 
import android.widget.RadioGroup; 
import android.widget.TextView; 
import android.widget.Toast; 
import android.widget.RadioGroup.OnCheckedChangeListener; 
import android.widget.RelativeLayout; 

public class Main9Activity extends Activity { 
    RadioGroup RG1,RG2,RG3; 
    RelativeLayout rl; 
    public String chec=""; 
    Button bt; 


    String status[] = { "Leave", "Late", "Absent", "Present" }; 
    // RadioGroup rg; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main9); 
     LinearLayout rl = (LinearLayout) findViewById(R.id.linear1); 
     // rl = (RelativeLayout) findViewById(R.id.linear1); 





     for (int k = 1; k < 4; k++) { 
      final RadioButton[] rb = new RadioButton[10]; 

      // rg = new RadioGroup(this); 
      final RadioGroup rg = new RadioGroup(this); // create the RadioGroup 
      rg.setOrientation(RadioGroup.HORIZONTAL);// or RadioGroup.VERTICAL 
      final TextView name=new TextView(this); 
      String texid="16roll"+k; 
      name.setText("16roll"+k); 
      name.setId(R.id.RG2+k); 

      int n = 4; 
      for (int i = 0; i < n; i++) { 
       rb[i] = new RadioButton(this); 
       rg.addView(rb[i]); 
       // rb[i].setText("Radio " + i); 
       rb[i].setText(status[i]); 

      } 
      //rl.addView(rg); 

      rl.addView(name); 
      rl.addView(rg); 
      rl.setPadding(50, 50, 50, 50); 
      rg.setOnCheckedChangeListener(new OnCheckedChangeListener() { 
       @Override 
       public void onCheckedChanged(RadioGroup group, int checkedId) { 
// TODO Auto-generated method stub 
        int pos = rg.indexOfChild(findViewById(checkedId)); 
        //String roll="ytutyuty"; 
        String roll=name.getText().toString(); 
        switch (pos) { 
         case 0: 

          Toast.makeText(getBaseContext(), String.format("Leave" + roll), Toast.LENGTH_SHORT).show(); 

          chec="Leave"+ roll; 
          break; 
         case 1: 
          Toast.makeText(getBaseContext(), String.format("Late" +roll), Toast.LENGTH_SHORT).show(); 
          chec="Late" +roll; 
          break; 
         case 2: 
          Toast.makeText(getBaseContext(), String.format("Absent" +roll), Toast.LENGTH_SHORT).show(); 
          chec="Absent" +roll; 
          break; 
         default: 
          Toast.makeText(getApplicationContext(), String.format("Present" +roll), Toast.LENGTH_SHORT).show(); 
          chec="Present" +roll; 
          break; 
        } 
       } 
      }); 
     } 
     // Toast.makeText(getBaseContext(), String.format(chec), Toast.LENGTH_SHORT).show(); 
     // @Override 
     //public boolean onCreateOptionsMenu(Menu menu) { 
     // getMenuInflater().inflate(R.menu.activity_main, menu); 
     // return true; 
     // } 


     bt= (Button) findViewById(R.id.button2); 
     bt.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       Toast.makeText(getBaseContext(), String.format(chec), Toast.LENGTH_SHORT).show(); 
       // 
      } 
     }); 


    } 
} 

回答

0

這是太複雜了。只需在活動的XML佈局中包含radiogroup即可。

然後,在按鈕單擊上,將onClick屬性設置爲您的活動中的某個功能。在那裏,你的RadioGroup中進行查找:

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

然後用這個: int idOfSelected = rg.getCheckedRadioButtonId();

現在你可以做一個選擇了其中的switch-case:

switch(idOfSelected) { 
    case R.id.radio1: 
     // Do stuff 
     break; 
} 

等。等等。

+0

謝謝,但我正在生成一個動態單選按鈕裏面的廣播組... – egom

+0

@egom答案的其餘部分仍然有效(如何獲得選擇) – Aenadon

相關問題