Get EditText value in a Toast

1

I'm starting on android and I'm doing a program in which I have several EditText and when I click on a button I send a Toast with all the information of each of the Edit text, how can I send it the toast but with the information of each of the EditText but in a Toast

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button butoon = (Button)findViewById(R.id.button);
        butoon.setOnClickListener(this);
    }

    public void onClick(View v)
    {
        EditText text = (EditText)findViewById(R.id.editText_Nombre);
        Toast toast = Toast.makeText(getApplicationContext(),"La reservacion ",Toast.LENGTH_SHORT
        );

        Toast(text);
        toast.show();
    }

    private Toast Toast(EditText text)
    {
        return null;
    }
}
    
asked by Elenasys 29.04.2017 в 06:34
source

3 answers

1

The second parameter that receives Toast.makeText() is a CharSequence . You can get the text of a EditText using EditText.getText() , and then convert it to String through the toString() method. In your code this would be:

public void onClick(View v)
{
    EditText text = (EditText)findViewById(R.id.editText_Nombre);
    Toast toast = Toast.makeText(getApplicationContext(), "La reservación " + text.getText().toString(), Toast.LENGTH_SHORT);

    toast.show();
}

Let me know if it works!

    
answered by 29.04.2017 в 09:33
1

First to use this method in this way, receiving a view as a parameter

public void onClick(View v){

You must call it from the property android:onClick from your Layout or your class implements the class onClicklistener , apparently it is like that since you are defining the listener in this way:

butoon.setOnClickListener(this);

To add the value of EditText just get it this way, getting the CharSequene and convert it to String using the method toString()

String valorEditText = text.getText().toString();

Your code would be this way

    public void onClick(View v)
    {
        EditText text = (EditText)findViewById(R.id.editText_Nombre);
        String valorEditText = text.getText().toString();  
       Toast.makeText(getApplicationContext(),"La reservacion " + valorEditText,Toast.LENGTH_SHORT).show();

       /*No es necesaria esta forma 
Toast toast = Toast.makeText(getApplicationContext(),"La reservacion ",Toast.LENGTH_SHORT
        );

        Toast(text);
        toast.show();*/
    }
    /*metodo no necesario
    private Toast Toast(EditText text)
    {
        return null;
    }*/

If you have several EditText get the text you have inside by

 String valorEditText1 = text1.getText().toString();
 String valorEditText2 = text2.getText().toString();
 String valorEditText3 = text3.getText().toString();
 ...
 ...

and concatenate them to the toast message.

    
answered by 29.04.2017 в 19:57
0

You must declare the text outside the click code and onCreate, so that you can reuse it anywhere you want, what you want to do is done with getText().toString() , leave a comment there and leave a link below if you want customize it even more.

EditText text;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        text = (EditText)findViewById(R.id.editText_Nombre);
        Button butoon = (Button)findViewById(R.id.button);
        butoon.setOnClickListener(this);
    }

    public void onClick(View v)
    {
     String dato = text.getText().toString();
     /* El metodo getText() obtiene el dato escrito con el metodo toString()
     se convierte a String para poder manipularlo como tal, por ultimo
     se muestra en el textView con el metodo .setText()
     */
     Toast toast = Toast.makeText(getApplicationContext(), dato,Toast.LENGTH_SHORT);

        toast.show();
    }

Interesting information here to customize one more the Toast

    
answered by 29.04.2017 в 09:28