简体   繁体   中英

How can I make a if condition inside a StringBuilder on android?

I have this function that sends an email from an android app:

 public void enviarEmail() {

    //Instanciamos los componentes a utilizar


    String[] TO = {"pereiraharles@gmail.com"};

    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setData(Uri.parse("mailto:"));
    intent.setType("text/html");
    intent.putExtra(Intent.EXTRA_EMAIL, TO);
    intent.putExtra(Intent.EXTRA_SUBJECT, "Relevamiento");
    intent.putExtra(Intent.EXTRA_TEXT,

            Html.fromHtml(new StringBuilder()
                    .append("<p><b>Fecha:</b>" + fecha.getText() + "</p>")
                    .append("<p><b>Obra:</b>" + obra.getText() + "</p>")
                    .append("<p><b>Lugar:</b>" + lugar.getText() + "</p>")
                    .append("<p><b>Color de vidrio:</b>" + color.getText() + "</p>")
                    .append("<p><b>Milimetraje:</b>" + milimetraje.getText() + "</p>")
                    .append("<p><b>Tipo de abertura:</b>" + abertura.getSelectedItem() + "</p>")
                    .append("<p><b>Largo:</b>" + largo.getText() + "</p>")
                    .append("<p><b>Ancho:</b>" + ancho.getText() + "</p>")
                    .append("<p><bold>Observacion:</bold>" + obs.getText() + "</p>")
                    .append("<p><b>Lugar:</b>" + lugar2.getText() + "</p>")
                    .append("<p><b>Color de vidrio:</b>" + color2.getText() + "</p>")
                    .append("<p><b>Milimetraje:</b>" + milimetraje2.getText() + "</p>")
                    .append("<p><b>Tipo de abertura:</b>" + abertura2.getSelectedItem() + "</p>")
                    .append("<p><b>Largo:</b>" + largo2.getText() + "</p>")
                    .append("<p><b>Ancho:</b>" + ancho2.getText() + "</p>")
                    .append("<p><bold>Observacion:</bold>" + obs2.getText() + "</p>")
                    .toString()

            )
    );


    try {
        startActivity(Intent.createChooser(intent, "Seleccione correo"));
        finish();
    } catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(MainActivity.this, "Error al enviar", Toast.LENGTH_SHORT).show();
    }

What I need to do is an if condition, so that if an EditText field is empty, it does not load to the email, instead if it has a loaded value, it goes in the email

You are doing:

Html.fromHtml(new StringBuilder()
        .append("foo")
        .append("bar")
        .append("baz")
        .toString()
);

If you want a condition on the bar entry, change code to use statements, instead of inlining it.

StringBuilder buf = new StringBuilder()
        .append("foo");
if (doBar)
    buf.append("bar");
buf.append("baz");
Html.fromHtml(buf.toString());

It is really that simple. Just normal Java code.

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