有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何将复选框值传递给动作\u发送

我正在尝试做我的第一个应用程序,我自学了Java,我在2个月前开始使用,所以请原谅我的错误。 我想将复选框的值传递给电子邮件文本,但我认为我需要在发送电子邮件之前刷新“某物”,因为这些值始终为false。。我不知道该怎么办。 代码如下:

public class Appuntamento extends Activity{
    String paziente;

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.appuntamento);
        //riceviamo id e lo mettiamo come nome utente
        final EditText nomePaziente = (EditText)findViewById(R.id.nomePaziente);
        Bundle dati = this.getIntent().getExtras();
        nomePaziente.setText(dati.getString("id"));
        final String id = dati.getString("id");
        EditText noteAppuntamento = (EditText)findViewById(R.id.noteAppuntamento);
        final String note = noteAppuntamento.getText().toString();

        final CheckBox lunedi = (CheckBox) findViewById(R.id.checkboxLunedi);
        final boolean lun = lunedi.isSelected();
        final CheckBox martedi = (CheckBox) findViewById(R.id.checkboxMartedì);
        final boolean mar = martedi.isSelected();
        final CheckBox mercoledi = (CheckBox) findViewById(R.id.checkboxMercoledi);
        final boolean mer = mercoledi.isSelected();
        final CheckBox giovedi = (CheckBox) findViewById(R.id.checkboxGiovedi);
        final boolean giov = giovedi.isSelected();
        final CheckBox venerdi = (CheckBox) findViewById(R.id.checkboxVenerdi);
        final boolean ven = venerdi.isSelected();


        StringBuilder testoMail = new StringBuilder();
        if (lun ){
            testoMail.append("Lunedì");
        } else if (mar){
            testoMail.append("Martedì");
        }else if (mer) {
            testoMail.append("Mercoledì");
        } else if (giov) {
            testoMail.append("Giovedì");
        } else if (ven) {
            testoMail.append("Venerdì");
        }

        final String giorni = testoMail.toString();


        Button richiestaAppuntamento = (Button)findViewById(R.id.btnRichiestaAppuntamento);
        richiestaAppuntamento.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent mail = new Intent(Intent.ACTION_SEND);
                mail.setType("message/rfc822");
                mail.putExtra(Intent.EXTRA_SUBJECT, "Richiesta appuntamento");
                mail.putExtra(Intent.EXTRA_TEXT, "Nome paziente: " + id + " " + giorni + " " + "Note: " + note);
                mail.putExtra(Intent.EXTRA_EMAIL, new String[] {"dottcastellitto@gmail.com"});
                startActivity(mail);
            }
        });

    }
}

共 (1) 个答案

  1. # 1 楼答案

    有两种方法可以做到这一点 像

    public class Appuntamento extends Activity
    { 
    String paziente;
      boolean lun,mar ,mer,giov,ven;
    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.appuntamento);
        //riceviamo id e lo mettiamo come nome utente
        final EditText nomePaziente = (EditText)findViewById(R.id.nomePaziente);
        Bundle dati = this.getIntent().getExtras();
        nomePaziente.setText(dati.getString("id"));
        final String id = dati.getString("id");
        EditText noteAppuntamento = (EditText)findViewById(R.id.noteAppuntamento);
        final String note = noteAppuntamento.getText().toString();
    
        final CheckBox lunedi = (CheckBox) findViewById(R.id.checkboxLunedi);
    
        final CheckBox martedi = (CheckBox) findViewById(R.id.checkboxMartedì);
    
        final CheckBox mercoledi =(CheckBox)findViewById(R.id.checkboxMercoledi);
    
        final CheckBox giovedi = (CheckBox) findViewById(R.id.checkboxGiovedi);
    
        final CheckBox venerdi = (CheckBox) findViewById(R.id.checkboxVenerdi); 
        Button richiestaAppuntamento =   (Button)     findViewById(R.id.btnRichiestaAppuntamento);
        richiestaAppuntamento.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            ven = venerdi.isChecked();
            lun = lunedi.isChecked();
            mar = martedi.isChecked();
            mer = mercoledi.isChecked();
            giov = giovedi.isChecked();
            StringBuilder testoMail = new StringBuilder();
    //your code this is fine if only one selected item info or data you want to send in mail 
            if (lun ){
             testoMail.append("Lunedì");
            } else if (mar){
              testoMail.append("Martedì");
            }else if (mer) {
             testoMail.append("Mercoledì");
            } else if (giov) {
             testoMail.append("Giovedì");
            } else if (ven) {
             testoMail.append("Venerdì");
            }
    // if you want all then comment above code and uncomment below code
    /*
                if (lun ){
                 testoMail.append("Lunedì");
                } 
                if (mar){
                  testoMail.append("Martedì");
                }
                if (mer) {
                 testoMail.append("Mercoledì");
                } 
                if (giov) {
                 testoMail.append("Giovedì");
                } 
                if (ven) {
                 testoMail.append("Venerdì");
                }
    */
    
          String giorni = testoMail.toString();
                Intent mail = new Intent(Intent.ACTION_SEND);
                mail.setType("message/rfc822");
                mail.putExtra(Intent.EXTRA_SUBJECT, "Richiesta appuntamento");
                mail.putExtra(Intent.EXTRA_TEXT, "Nome paziente: " + id + " " + giorni + " " + "Note: " + note);
                mail.putExtra(Intent.EXTRA_EMAIL, new String[] {"dottcastellitto@gmail.com"});
                startActivity(mail);
            }
        });
    
    }
    }
    

    另一种方式是 像

            public class Appuntamento extends Activity
            { 
    
            String paziente;
              boolean lun,mar ,mer,giov,ven;
            @Override
            protected void onCreate (Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.appuntamento);
                //riceviamo id e lo mettiamo come nome utente
                final EditText nomePaziente = (EditText)findViewById(R.id.nomePaziente);
                Bundle dati = this.getIntent().getExtras();
                nomePaziente.setText(dati.getString("id"));
                final String id = dati.getString("id");
                EditText noteAppuntamento = (EditText)findViewById(R.id.noteAppuntamento);
                final String note = noteAppuntamento.getText().toString();
    
                final CheckBox lunedi = (CheckBox) findViewById(R.id.checkboxLunedi);
               lunedi.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    
               @Override
               public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
        lun=isChecked;
        //either one above or below
        //lun = lunedi.isChecked();
               }
           }
        );     
                final CheckBox martedi = (CheckBox) findViewById(R.id.checkboxMartedì);
              martedi.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    
               @Override
               public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
        mar=isChecked;
        //either one above or below
        //mar = martedi.isChecked();
               }
           }
        );     
                final CheckBox mercoledi =(CheckBox)findViewById(R.id.checkboxMercoledi);
                 mercoledi.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    
               @Override
               public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
        mer=isChecked;
        //either one above or below
        // mer = mercoledi.isChecked();
               }
           }
        );     
                final CheckBox giovedi = (CheckBox) findViewById(R.id.checkboxGiovedi);
                giovedi.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    
               @Override
               public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
         giov=isChecked;
        //either one above or below
        // giov = giovedi.isChecked();
               }
           }
        );     
                final CheckBox venerdi = (CheckBox) findViewById(R.id.checkboxVenerdi); 
        venerdi.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    
               @Override
               public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
         ven=isChecked;
        //either one above or below
        //ven = venerdi.isChecked();
               }
           }
        );     
                Button richiestaAppuntamento =   (Button)     findViewById(R.id.btnRichiestaAppuntamento);
                richiestaAppuntamento.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                  //  ven = venerdi.isChecked();
                  //  lun = lunedi.isChecked();
                   // mar = martedi.isChecked();
                   // mer = mercoledi.isChecked();
                  //  giov = giovedi.isChecked();
                    StringBuilder testoMail = new StringBuilder();
            //your code this is fine if only one selected item info or data you want to send in mail 
                    if (lun ){
                     testoMail.append("Lunedì");
                    } else if (mar){
                      testoMail.append("Martedì");
                    }else if (mer) {
                     testoMail.append("Mercoledì");
                    } else if (giov) {
                     testoMail.append("Giovedì");
                    } else if (ven) {
                     testoMail.append("Venerdì");
                    }
            // if you want all then comment above code and uncomment below code
            /*
                        if (lun ){
                         testoMail.append("Lunedì");
                        } 
                        if (mar){
                          testoMail.append("Martedì");
                        }
                        if (mer) {
                         testoMail.append("Mercoledì");
                        } 
                        if (giov) {
                         testoMail.append("Giovedì");
                        } 
                        if (ven) {
                         testoMail.append("Venerdì");
                        }
            */
    
                  String giorni = testoMail.toString();
                        Intent mail = new Intent(Intent.ACTION_SEND);
                        mail.setType("message/rfc822");
                        mail.putExtra(Intent.EXTRA_SUBJECT, "Richiesta appuntamento");
                        mail.putExtra(Intent.EXTRA_TEXT, "Nome paziente: " + id + " " + giorni + " " + "Note: " + note);
                        mail.putExtra(Intent.EXTRA_EMAIL, new String[] {"dottcastellitto@gmail.com"});
                        startActivity(mail);
                    }
                });
    
            }
            }