下面的代码片段显示了一个带有简单登录表单的Dialog.问题是当用户点击登录按钮时,输入到EditTexts的文本不会在getText() – 调用上返回.但是,如果我在xml-layout的EditTexts上设置 android:set
private void showLoginDisplay() { loginDialog = new Dialog(GOFdroid.this); loginDialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND, WindowManager.LayoutParams.FLAG_BLUR_BEHIND); loginDialog.setTitle("Logga in"); LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); View dialogView = li.inflate(R.layout.login_dialog, null); loginDialog.setContentView(dialogView); loginDialog.show(); Button loginButton = (Button) dialogView.findViewById(R.id.login_button); Button cancelButton = (Button) dialogView.findViewById(R.id.cancel_button); EditText unameView = (EditText) dialogView.findViewById(R.id.uname_id); if (unameView != null) Log.d(TAG, "unameView != null"); Log.d(TAG, "uname.getText(): " + unameView.getText().toString()); uname = unameView.getText().toString(); EditText pwdView = (EditText) dialogView.findViewById(R.id.pwd_id); if (pwdView != null) pwd = pwdView.getText().toString(); Log.d(TAG, "uname = " + uname + ", pwd = " + pwd); loginButton.setOnClickListener(new OnClickListener() { //@Override public void onClick(View v) { Log.d(TAG, "Clicked <logga in>"); loginDialog.dismiss(); viewEvents = new Runnable() { //@Override public void run() { getEvents(uname, pwd); } }; Thread thread = new Thread(null, viewEvents, "MagentoBackground"); thread.start(); pDialog = ProgressDialog.show(GOFdroid.this, "Uppdaterar...", "Hämtar registrerade körningar...", true); } }); }
和XML:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingLeft="10dip" android:paddingRight="10dip" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/uname_label"/> <EditText android:id="@+id/uname_id" android:layout_width="220dip" android:layout_height="wrap_content" android:text="foo" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/pwd_label" android:paddingTop="10dip"/> <EditText android:id="@+id/pwd_id" android:layout_width="220dip" android:layout_height="wrap_content" android:text="" android:inputType="textPassword" /> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingTop="15dip" > <Button android:id="@+id/login_button" android:layout_width="100dip" android:layout_height="fill_parent" android:text="Logga in" /> <Button android:id="@+id/cancel_button" android:layout_width="100dip" android:layout_height="fill_parent" android:text="Avbryt" /> </LinearLayout> </LinearLayout>问题是你没有在匿名内部类中更新uname和pwd.只有在调用showLoginDisplay()方法时才更新uname和pwd.当用户更改文本并点击登录按钮时,将调用onClick()方法.
在你想要做的内部类的onClick()方法中:
uname = unameView.getText(); pwd = pwdView.getText();
您必须创建unameView和pwdView全局/字段变量,因此匿名内部类可以访问它们.
你必须这样做的原因是因为当按下登录按钮时,只运行具有匿名内部类的onClick的代码.
我希望能解决这个问题.