简体   繁体   中英

EditText.getText() is not working properly in Android

I have the following code, but I am not able to retrieve the values entered in the edit text boxes ( userInput_lat and userInput_lon ) to the strings latitude and longitude using this:

latitude = userInput_lat.getText().toString();
longitude = userInput_lon.getText().toString();
result.setText(latitude);

It prints nothing. Would you please suggest a remedy?

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class Prompt extends Activity {

    final Context context = this;
    private Button button, button2;
    private EditText result, result2, userInput_lat, userInput_lon;

    String latitude, longitude, phone;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_prompt);

        // components from main.xml
        button = (Button) findViewById(R.id.loc);
        result = (EditText) findViewById(R.id.editTextResult);
        result2 = result;
        // add button listener
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                // get prompts.xml view
                LayoutInflater li = LayoutInflater.from(context);
                View promptsView = li.inflate(R.layout.prompts, null);
                AlertDialog.Builder alertDialogBuilder_l = new AlertDialog.Builder(context);

                // set prompts.xml to alertdialog builder
                alertDialogBuilder_l.setView(promptsView);

                // final EditText userInput_lat = (EditText)
                // promptsView.findViewById(R.id.latInput);
                // final EditText userInput_lon = (EditText)
                // promptsView.findViewById(R.id.lonInput);
                userInput_lat = (EditText) promptsView.findViewById(R.id.latInput);
                userInput_lon = (EditText) promptsView.findViewById(R.id.lonInput);

                /**
                 * 
                 * code to get input to a string
                 */
                latitude = userInput_lat.getText().toString();
                longitude = userInput_lon.getText().toString();

                // set dialog message
                alertDialogBuilder_l.setCancelable(false)
                        .setPositiveButton("SET", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int id) {
                                // get user input and set it to result edit text

                                result.setText(latitude);
                            }
                        }).setNegativeButton("BACK", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                            }
                        });

                // create alert dialog
                AlertDialog alertDialog = alertDialogBuilder_l.create();

                // show it
                alertDialog.show();

            }
        });
        // ////////To set phone nymber,actions to be performed while clicking
        // 3rd button////
        button2 = (Button) findViewById(R.id.ph);
        result2 = (EditText) findViewById(R.id.editTextResult);

        // add button listener
        button2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                // get prompts.xml view
                LayoutInflater li2 = LayoutInflater.from(context);
                View prompts_phView = li2.inflate(R.layout.prompts_ph, null);

                AlertDialog.Builder alertDialogBuilder_p = new AlertDialog.Builder(context);
                alertDialogBuilder_p.setView(prompts_phView);

                final EditText userInput_ph = (EditText) prompts_phView.findViewById(R.id.phInput);
                // code to get input to a string
                // Log.v("EditText", userInput_ph.getText().toString());
                phone = userInput_ph.getText().toString();

                // set dialog message
                alertDialogBuilder_p.setCancelable(false)
                        .setPositiveButton("SET", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int id) {
                                // get user input and set it to result
                                // edit text
                                result2.setText(userInput_ph.getText());
                            }
                        }).setNegativeButton("BACK", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                            }
                        });

                // create alert dialog
                AlertDialog alertDialog_p = alertDialogBuilder_p.create();

                // show it
                alertDialog_p.show();

            }
        });

    }

}

This is happening because the code that reads the EditText values is called when those fields are made, not when the user clicks the accept button in the popup.

Logically, the flow as it stands is:

  1. Inflate popup layout
  2. Get EditText [branch new at this point]
  3. Read text
  4. Display popup
  5. User taps "SET"
  6. Result is set to the latitude value

The problem is that #3 happens before #5. Move that code into the same place that you call result.setText(latitude) , and you should be fine. Something like this:

.setPositiveButton("SET", new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int id) {
        // get user input and set it to result edit text
        latitude = userInput_lat.getText().toString();
        result.setText(latitude);
    }

}

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