简体   繁体   中英

How to add a value to a Toast, from a Edittext (with button)

I searched the web, but couldn't find a solution. I found several codes but I have some problems to implement it in my code. Hope you guys know what I'm messing up here.

I'm creating an SMS app, where you choose from a spinner (which preloads a txt) and you can continue the text from an edittextfield and press the button to send the SMS. Working great but now I would like the toast to contain what the user wrote in the field.I can create a normal Toast where I can write my own text. If you look at case 1 you can see where I wrote value_edittextfield (just so you can see where the value should be) and String nrforanvandare is the EditTextField.

I really hope there is a solution, because it would be so awesome.

 spinneruse.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                switch (position) {









                    case 0 :
                        skickatelBTN.setOnClickListener(new View.OnClickListener() {
                                                            @Override
                                                            public void onClick(View v) {


                                                            }

                                                        }
                        );






                    case 1 :
                        skickatelBTN.setOnClickListener(new View.OnClickListener() {
                                                          @Override
                                                          public void onClick(View v) {
                                                              String myMsgnruta = tele1txt.getText().toString();
                                                              String theNumberr = nyanumtxt.getText().toString();
                                                              String nrforanvandare = nrrutaforspinner.getText().toString();
                                                              sendMsg(theNumberr, myMsgnruta + nrforanvandare);
                                                              Toast.makeText(getActivity(), "value_Edittextfield. sent",
                                                                      Toast.LENGTH_LONG).show();
                                                          }

                                                      }
                        );
                        break;

According to EditText you can use getText() to get the input text, which in turn returns Editable , which you can get with the default toString() method.

For example:

Toast.makeText(getApplicationContext(), myEditText.getText().toString(),
    Length_LONG).show();

Here, I assumed that your EditText variable name is myEditText .

This should do it.

EDIT:

On a side note, why wouldn't using String nrforanvandare = nrrutaforspinner.getText().toString(); do the trick, given nrrutaforspinner is your EditText? If not, well, that's how you do it.

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