2009-09-22 145 views
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> 

回答

6

的問題是,你是不是匿名內部類更新UNAME和pwd。當調用showLoginDisplay()方法時,您只更新uname和pwd。當用戶更改文本並點擊登錄按鈕時,會調用onClick()方法。

在你想要做的內部類的onclick()方法:

uname = unameView.getText(); 
pwd = pwdView.getText(); 

你將不得不作出unameViewpwdView全局/字段變量,所以他們是通過匿名內部類accessable。

您必須這樣做的原因是,當按下登錄按鈕時,只會運行匿名內部類的onClick代碼。

我希望能解決這個問題。

+0

猜猜這就是你所說的菜鳥錯誤..謝謝,完美的作品! – aspartame 2009-09-23 00:09:11