簡體   English   中英

單擊按鈕時如何從android的Radio組獲取選定的多個單選按鈕值的數據

[英]how to get data of selected multiple radio button values from a Radio Group in android when a button is clicked

如何在單擊按鈕時從android的Radio組中獲取選定的多個單選按鈕值的數據。並顯示所有數據。代碼正在運行,但在提交按鈕時卡住了..但未獲取數據。

這是代碼:

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();
                //
            }
        });


    }
}

這太復雜了。 只需在活動的XML布局中包含單選組即可。

然后,在單擊按鈕時,將onClick屬性設置為活動中的函數。 在那里,您可以查詢廣播組:

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

然后使用此方法: int idOfSelected = rg.getCheckedRadioButtonId();

現在,您可以選擇一個開關盒:

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

etc.etc。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM