3
下面的代碼片段顯示了一個帶有簡單登錄窗體的對話框。問題是,當用戶點擊登錄按鈕時,輸入到EditTexts中的文本不會在getText()調用中返回。但是,如果我在xml佈局中的EditTexts上設置了android:setText =「foo」,則getText()將返回「foo」。任何想法爲什麼在運行時輸入的文本不會粘住?EditText不返回getText上的內容()
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>
猜猜這就是你所說的菜鳥錯誤..謝謝,完美的作品! – aspartame 2009-09-23 00:09:11