[英]EditText.getText() is not working properly in Android
我有下面的代碼,但我不能夠檢索在編輯文本框(輸入的值userInput_lat
和userInput_lon
)的字符串latitude
和longitude
使用這樣的:
latitude = userInput_lat.getText().toString();
longitude = userInput_lon.getText().toString();
result.setText(latitude);
它什么也不打印。 您能建議一種補救方法嗎?
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();
}
});
}
}
之所以發生這種情況,是因為在創建這些字段時會調用讀取EditText值的代碼,而不是在用戶單擊彈出窗口中的接受按鈕時調用。
從邏輯上講,當前的流程是:
問題是#3發生在#5之前。 將該代碼移到您調用result.setText(latitude)
,就可以了。 像這樣:
.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);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.