Show a string in a Alertdialog

Asked

Viewed 95 times

0

Well, I’m learning android, I don’t know much. In this application, so far I just want to present the value of "name" in . Alertdialog setMessage, and I’m not sure how to do it, thanks in advance!

NOTE: When I run the app on my device, it appears
"Its name is:android.support.v7.widget.Appcompatedittext{...}"

package usuario.app.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.*;
import android.app.*;
import android.view.*;


public class ComprasActivity extends AppCompatActivity {

EditText nome, idade,cidade;
Button enviar;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_compras);

    nome    = (EditText) findViewById(R.id.nome);
    idade   = (EditText) findViewById(R.id.idade);
    cidade  = (EditText) findViewById(R.id.cidade);
    enviar = (Button)findViewById(R.id.enviar);

    enviar.setOnClickListener(new View.OnClickListener()
    {
                @Override
                public void onClick(View arg0)
                {
                    AlertDialog.Builder dialogo = new AlertDialog.Builder(ComprasActivity.this);
                    dialogo.setTitle("Seu perfil");
                    dialogo.setMessage("Seu nome é:" + nome);
                    dialogo.setNeutralButton("OK",null);
                    dialogo.show();
                }
    });
}

}

1 answer

4


The variable nome is the type EditText, representing an element of the layout. To access the text it contains it is necessary to call the method getText():

dialogo.setMessage("Seu nome é:" + nome.getText());

It is important to stress that the method getText() returns the text as charSequence and not String, which although in this case it is simple to function, in others you will find later may no longer work.

To get the text of a EditText in the form of String, must use:

nome.getText().toString()

An example of this would be what the extension indicated, to assign the text of the EditText to a variable of the type String. Then it would have to be:

String nomeRecolhido = nome.getText().toString();
  • I was about to reply but you anticipated it. However your answer is not completely correct.

  • @ramaral Forget something?

  • Not exactly, because in this case it works. But the "most correct" would be to use nome.getText().toString().

  • Thank you very much. The start is never easy, thanks for the help.

  • @ramaral Yes I did, but I ended up walking back and pulled out to be simpler. But in several other places it is necessary a String it has to be the way it indicated, since getText return a charSequence and not a String

  • It may be simpler but the AP (which is still learning) will be confused when using only getText() when assigning to a variable String type and error.

  • @ramaral Yes you are right, I can complete the answer with that reference, to help in the future in other similar situations.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.