2017-02-15 53 views
0

我對發現錯誤感到厭倦。我沒有發現任何錯誤,但我沒有從editText獲取文本。請看下面的代碼:對話框上的EditText沒有返回任何文字

activity_pwd.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/layout_root" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="vertical" 
android:padding="10dp" > 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Enter PIN " 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

<EditText 
    android:id="@+id/pwdValue" 
    android:layout_width="match_parent" 
    android:maxLength="4" 
    android:maxLines="1" 
    android:inputType="numberPassword" 
    android:layout_height="wrap_content"> 
</EditText> 
</LinearLayout> 

我叫LockImmediately(本);方法的onCreate MainActivity

public void LockImmediately(Context context) { 
    if (lock_app) { 
     View myview = LayoutInflater.from(context).inflate(R.layout.activity_pwd,null); 
     enterPWD = (EditText) myview.findViewById(R.id.pwdValue); 

     AlertDialog alertDialog = new AlertDialog.Builder(this).setCancelable(false) 
       .setView(R.layout.activity_pwd).setPositiveButton("OK", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         String user_text = enterPWD.getText().toString(); 
         Log.d("onCreate pws", " i "+user_text); 
         if (pwd.equals(user_text)) { 
          dialog.dismiss(); 
          Log.d("onCreate", "Work done"); 
         } else { 
          dialog.dismiss(); 
          MainActivity.this.finish(); 
         } 
        } 
       }).create(); 
     alertDialog.show(); 
    } 
} 

Log.d

02-15 23:15:30.840 29379-29379/com.developersqueen.wishlater D/onCreate: onCreate 
02-15 23:15:37.021 29379-29379/com.developersqueen.wishlater D/onCreate pws: i 
02-15 23:15:45.427 29379-29379/com.developersqueen.wishlater D/onCreate: onCreate 
02-15 23:15:49.026 29379-29379/com.developersqueen.wishlater D/onCreate pws: i 

你可以看到上面那個日誌我有 「我」,但它沒有返回值連接的EditText值..我已經嘗試清除數據,unistalling應用程序,乾淨,建立一切,但總是相同的結果!任何幫助,將不勝感激..

+0

嘗試更改'enterPWD.getText()。toString();'alertDialog.findViewById(R.id.pwdValue).getText()。toString();',看它是否有效 – Sanjeet

+0

不,它給出錯誤對編譯非法字符\ u200c –

+0

然後刪除那個非法字符並試試 – Sanjeet

回答

1

當您的視圖設置爲AlertDialog,你應該設置視圖,在這裏找到了EditText上的ID。

您已經使用myview查找EditText的ID,但是您嘗試從Activity獲取myView,而不是從Dialog獲取。

如果你設置視圖是MyView的,你可以從enterPWD的EditText文本。

AlertDialog alertDialog = new AlertDialog.Builder(this).setCancelable(false) 
       .setView(myview).setPositiveButton("OK", new DialogInterface.OnClickListener() { 
... 
} 

您應該爲活動和對話框本身創建單獨的佈局。不要爲兩者使用一個佈局。

+0

非常感謝你!這工作像一個魅力!^_ ^。 –

0

您哄擡View拖放,

View myview = LayoutInflater.from(context).inflate(R.layout.activity_pwd,null);不是對話的View,它的無用的實例。

您將佈局的ID傳遞給對話框,該對話框在.show()中將其自己的版本膨脹。 直接使用alertDialog.findViewById(R.id.pwdValue)代替onClick(..)

+0

讓我檢查它 –

+0

拋出空指針異常 –

+0

在哪一行?我只是複製粘貼它,它的工作 – CrowsNet

0

試試這個

public void LockImmediately(Context context) { 
    if (lock_app) { 
     AlertDialog alertDialog = new AlertDialog.Builder(this).setCancelable(false) 
       .setView(R.layout.activity_pwd).setPositiveButton("OK", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
          EditText enterPWD = (EditText) myview.findViewById(R.id.pwdValue); 
          String user_text = enterPWD.getText().toString(); 
          Log.d("onCreate pws", " i "+user_text); 
          if (pwd.equals(user_text)) { 
           dialog.dismiss(); 
           Log.d("onCreate", "Work done"); 
          } else { 
           dialog.dismiss(); 
           MainActivity.this.finish(); 
          } 
         } 
        }).create(); 
      alertDialog.show(); 
     } 
    } 
+0

你做了什麼改變先生? –

+0

我刪除你的佈局充氣和初始編輯文本里麪點擊@appleappala –