2009-09-22 2 views
3

아래 코드 스 니펫은 간단한 로그인 양식이있는 대화 상자를 표시합니다. 문제는 사용자가 로그인 버튼을 눌렀을 때 EditText에 입력 된 텍스트가 getText() 호출에서 반환되지 않는다는 것입니다. 그러나, 안드로이드 설정 : setText = "foo"는 xml- 레이아웃 "foo"의 EditTexts에서 getText()에 반환됩니다. 런타임에 입력 한 텍스트가 왜 붙지 않는지 알기 원하십니까? 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 글로벌/필드 변수를해야 할 것이다, 그래서 그들은 익명의 내부 클래스에 의해시켜 액세스.

이렇게해야하는 이유는 로그인 버튼을 누르면 익명 내부 클래스의 onClick이있는 코드 만 실행되기 때문입니다.

문제가 해결되기를 바랍니다.

+0

신기한 실수라고 생각합니다. 감사합니다. 완벽하게 작동합니다! – aspartame

관련 문제