如何从Xamarin Android中的radiobutton获取值?

时间:2017-09-28 14:57:39

标签: c# android xamarin xamarin.android radio-button

我需要获取radiobutton的文本。在代码中有4个无线电组和13个无线电按钮,如果有人选择按钮3,我需要将该文本发送到变量,然后向已投票的人发送电子邮件+用CC向他的经理发送该值。

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



   }
 }

我应该使用rdbIntegridade.getCheckedRadioButtonId(); ?

我还想知道如何创建一个按钮来发送通过radiobuttons值的电子邮件+一个将在EditText字段中的注释。

1 个答案:

答案 0 :(得分:1)

  

我需要获取radiobutton的文本。

您可以订阅每个Click的{​​{1}}事件,例如:

RadioButton

RadioButton rbtn = FindViewById<RadioButton>(Resource.Id.rbtn); rbtn.Click += Rbtn_Click; 事件中:

Click

因此,您可以使用private void Rbtn_Click(object sender, EventArgs e) { var radiobtn = sender as RadioButton; var text = radiobtn.Text; } 之类的相同处理程序订阅Click的所有RadioButton个事件。

  

我还想知道如何创建一个按钮来发送通过radiobuttons值的电子邮件+一个将在EditText字段中的注释。

要发送电子邮件部分,您可以参考官方文档Send an Email

Rbtn_Click