简体   繁体   中英

How to get a value from a radiobutton in Xamarin Android?

I need to get the text of a radiobutton. In the code there are 4 radiogroups and 13 radiobuttons, if someone choose the button 3 I need that text to send to a variable and then to send an email to the voted person + with CC to his manager with that value.

  using Android.App;
  using Android.Widget;
  using Android.OS;
  using System.Collections;
  using System;

   namespace WhrReconigzer
   {
     [Activity(Label = "WhrReconigzer", MainLauncher = true)]
     public class MainActivity : Activity
    {
       private ArrayList enderecos;
       TextView queroreconhecer;
       TextView crie;
       TextView conquiste;
       TextView entregue;
       TextView viva;
       TextView comentar;
       EditText comentário;
       Spinner spinner;
       ArrayAdapter adapter;
       RadioButton rdbTerPaixao;
       RadioButton rdbSerOusado;
       RadioButton rdbJogueParaGanhar;
       RadioButton rdbLiberarOsTalentos;
       RadioButton rdbUmaWhirlpool;
       RadioButton rdbLiderarComVelocidade;
       RadioButton rdbSeroDono;
       RadioButton rdbSerFranco;
       RadioButton rdbRespeito;
       RadioButton rdbIntegridade;
       RadioButton rdbDiversidadeeInclusao;
       RadioButton rdbTrabalhoDeEquipe;
       RadioButton rdbEspiritoDeVitoria;
       RadioGroup rdgcrie;
       RadioGroup rdgconquiste;
       RadioGroup rdgentregue;
       RadioGroup rdgviva;

    protected override void OnCreate(Bundle savedInstanceState)
    {

        base.OnCreate(savedInstanceState);

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);
        //preenche o arraylist com os dados
        GetEmails();
        //cria a instância do spinner declarado no arquivo Main
        spinner = FindViewById<Spinner>(Resource.Id.spnDados);
        //cria textview
        queroreconhecer = FindViewById<TextView>(Resource.Id.txtReconhecer);
        crie = FindViewById<TextView>(Resource.Id.txtCrie);
        conquiste = FindViewById<TextView>(Resource.Id.txtConquiste);
        entregue = FindViewById<TextView>(Resource.Id.txtEntregue);
        viva = FindViewById<TextView>(Resource.Id.txtViva);
        comentar = FindViewById<TextView>(Resource.Id.txtComentário);
        comentário = FindViewById<EditText>(Resource.Id.edtComentario);
        rdbTerPaixao = FindViewById<RadioButton>(rdgcrie.CheckedRadioButtonId);
        rdbSerOusado = FindViewById<RadioButton>(rdgcrie.CheckedRadioButtonId);
        rdbJogueParaGanhar = FindViewById<RadioButton>(rdgcrie.CheckedRadioButtonId);
        rdbLiberarOsTalentos = FindViewById<RadioButton>(rdgconquiste.CheckedRadioButtonId);
        rdbUmaWhirlpool = FindViewById<RadioButton>(rdgconquiste.CheckedRadioButtonId);
        rdbLiderarComVelocidade = FindViewById<RadioButton>(rdgentregue.CheckedRadioButtonId);
        rdbSeroDono = FindViewById<RadioButton>(rdgentregue.CheckedRadioButtonId);
        rdbSerFranco = FindViewById<RadioButton>(rdgentregue.CheckedRadioButtonId);
        rdbRespeito = FindViewById<RadioButton>(rdgviva.CheckedRadioButtonId);
        rdbIntegridade = FindViewById<RadioButton>(rdgviva.CheckedRadioButtonId);
        rdbDiversidadeeInclusao = FindViewById<RadioButton>(rdgviva.CheckedRadioButtonId);
        rdbTrabalhoDeEquipe = FindViewById<RadioButton>(rdgviva.CheckedRadioButtonId);
        rdbEspiritoDeVitoria = FindViewById<RadioButton>(rdgviva.CheckedRadioButtonId);
        rdgcrie = FindViewById<RadioGroup>(Resource.Id.rdgCrie);
        rdgconquiste = FindViewById<RadioGroup>(Resource.Id.rdgConquiste);
        rdgentregue = FindViewById<RadioGroup>(Resource.Id.rdgEntregue);
        rdgviva = FindViewById<RadioGroup>(Resource.Id.rdgViva);
        // cria o adapter usando o leiaute SimpleListItem e o arraylist
         adapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleListItem1, enderecos);
        //vincula o adaptador ao controle spinner
        spinner.Adapter = adapter;
        //define o evento ItemSelected para exibir o item selecionado
        spinner.ItemSelected += Spinner_ItemSelected;

        if (rdbDiversidadeeInclusao.getCheckedRadioButtonId() != -1)
        {
            int id = rg1.getCheckedRadioButtonId();
            View radioButton = rg1.findViewById(id);
            int radioId = radioGroup.indexOfChild(radioButton);
            RadioButton btn = (RadioButton)rg1.getChildAt(radioId);
            String selection = (String)btn.getText();
        }

    }
    //colocar o nome ao invés do email
    private void GetEmails()
    {
        enderecos = new ArrayList();
        enderecos.Add("alexandre_bonfim@whirlpool.com");
        enderecos.Add("alexandre_t_pires@whirlpool.com");
        enderecos.Add("ana_carolina_simoes @whirlpool.com");
        enderecos.Add("ana_claudia_s_belarmino@whirlpool.com");
        enderecos.Add("andre_costa@whirlpool.com");
        enderecos.Add("andre_l_teixeira@whirlpool.com");
        enderecos.Add("andreza_a_valle@whirlpool.com");
        enderecos.Add("anna_carolina_b_ferreira@whirlpool.com");
        enderecos.Add("bruno_b_souza@whirlpool.com");
        enderecos.Add("bruno_c_castanho@whirlpool.com");
        enderecos.Add("bruno_s_lombardero@whirlpool.com");
        enderecos.Add("caio_c_sacoman@whirlpool.com");
        enderecos.Add("carla_sedin@whirlpool.com");
        enderecos.Add("cassia_r_nascimento@whirlpool.com");
        enderecos.Add("celia_r_araujo@whirlpool.com");
        enderecos.Add("cesar_leandro_de_oliveira@whirlpool.com");
        enderecos.Add("daniel_b_szortyka@whirlpool.com");
        enderecos.Add("denis_caciatori@whirlpool.com");
        enderecos.Add("elisabete_c_ferreira@whirlpool.com");
        enderecos.Add("erick_c_senzaki@whirlpool.com");
        enderecos.Add("erika_g_souza@whirlpool.com");
        enderecos.Add("fabiana_monteiro@whirlpool.com");
        enderecos.Add("fernando_v_santos@whirlpool.com");
        enderecos.Add("gabriel_roveda@whirlpool.com");
        enderecos.Add("herivelto_alves_jr@whirlpool.com");
        enderecos.Add("jefferson_s_pecanha@whirlpool.com");
        enderecos.Add("josiane_a_teles@whirlpool.com");
        enderecos.Add("juliana_g_saito@whirlpool.com");
        enderecos.Add("juliano_ventola@whirlpool.com");
        enderecos.Add("leonardo_l_costa@whirlpool.com");
        enderecos.Add("leonardo_r_silva@whirlpool.com");
        enderecos.Add("lucas_m_santos@whirlpool.com");
        enderecos.Add("luiz_perea@whirlpool.com");
        enderecos.Add("norma_raphaeli@whirlpool.com");
        enderecos.Add("patricia_f_prates@whirlpool.com");
        enderecos.Add("priscila_l_dattilo@whirlpool.com");
        enderecos.Add("priscila_m_konte@whirlpool.com");
        enderecos.Add("reider_a_bernucio@whirlpool.com");
        enderecos.Add("renato_occhiuto@whirlpool.com");
        enderecos.Add("ricardo_a_fernandes@whirlpool.com");
        enderecos.Add("ricardo_matos_campaneruti @whirlpool.com");
        enderecos.Add("rogerio_pagotto@whirlpool.com");
        enderecos.Add("ruben_c_anacleto@whirlpool.com");
        enderecos.Add("taise_azevedo@whirlpool.com");
        enderecos.Add("vinicius_marques_assis@whirlpool.com");
        enderecos.Add("wanderly_t_limeira@whirlpool.com");
    }// fim getEmails

    private void Spinner_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
    {
        Spinner spinner = (Spinner)sender;
        string toast = string.Format("Colaborador selecionado: {0}", spinner.GetItemAtPosition(e.Position));
        Toast.MakeText(this, toast, ToastLength.Long).Show();
    }



   }
 }

Should I use rdbIntegridade.getCheckedRadioButtonId(); ?

And I also would like to know how to create a button to send an email passing the radiobuttons values + a comment that will be in a EditText field.

I need to get the text of a radiobutton.

You can subscribe the Click event of each RadioButton , for example like this:

RadioButton rbtn = FindViewById<RadioButton>(Resource.Id.rbtn);
rbtn.Click += Rbtn_Click;

In the Click event:

private void Rbtn_Click(object sender, EventArgs e)
{
    var radiobtn = sender as RadioButton;
    var text = radiobtn.Text;
}

So you can subscribe all Click events of RadioButton with the same handler here like Rbtn_Click .

And I also would like to know how to create a button to send an email passing the radiobuttons values + a comment that will be in a EditText field.

For sending Email part, you can refer to the official document Send an Email .

//send to
email.PutExtra (Android.Content.Intent.ExtraEmail,
new string[]{"person1@xamarin.com", "person2@xamrin.com"} );
//cc to
email.PutExtra (Android.Content.Intent.ExtraCc,
new string[]{"person3@xamarin.com"} );
//subject
email.PutExtra (Android.Content.Intent.ExtraSubject, "Hello Email");
//content
email.PutExtra (Android.Content.Intent.ExtraText,
"Hello from Xamarin.Android");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM