如何从 Edittext(带按钮)向 Toast 添加值

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

我在网上搜索过,但找不到解决方案。我找到了几个代码,但在我的代码中实现它时遇到了一些问题。希望你们知道我在这里搞砸了什么。

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.

我真的希望有一个解决方案,因为它会非常棒。

 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;

根据EditText you can use getText()获取输入文本,依次returnsEditable,可以用默认的toString()方法获取。

例如:

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

这里,我假设你的EditText变量名是myEditText

这应该可以做到。

编辑:

附带说明一下,鉴于 nrrutaforspinner 是您的 EditText,为什么不使用 String nrforanvandare = nrrutaforspinner.getText().toString(); 来解决问题?如果没有,那么,这就是你的做法。