2014-01-08 20 views
0

我正在嘗試創建一個對話框,其中包含2個用於創建密碼的edittexts。這裏是我的代碼:比較2 EditText條目並在Android中的對話框上顯示錯誤

AlertDialog.Builder alert = new AlertDialog.Builder(this); 
      alert.setCancelable(false); 
      alert.setTitle(title); 
      alert.setMessage(message); 
      LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE); 
      LinearLayout layout = (LinearLayout) layoutInflater.inflate(R.layout.create_pass, null); 
      final EditText password = (EditText) layout.findViewById(R.id.password); 
      final EditText passconfirm = (EditText) layout.findViewById(R.id.repeat_password); 
      password.setMinEms(6); 
      alert.setView(layout); 
      alert.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int whichButton) { 
        if (!password.getText().toString().equals(passconfirm.getText().toString())) { 
         passconfirm.setError(getString(R.string.password_mismatch)); 
         return; 
        } else if (password.getText().toString().isEmpty()) { 
         password.setError(getString(R.string.password_empty)); 
         return; 
        } else { 
         secprefs.putString("master_password", password.getText().toString()); 
        } 
       } 
      }); 
      alert.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int whichButton) { 
        dialog.cancel(); 
       } 
      }); 

      alert.show(); 

對話框工作正常,並且如果遵守2條件,密碼被存儲。 現在,如果不滿足2個條件,則對話框關閉。 我想撤消對話框關閉並顯示小彈出錯誤。有人知道爲什麼

我想設置的另一件事是密碼的最小長度。我嘗試setMinEms(6),但它不工作。

由於

+0

只是一個查詢,你在哪裏創建一個對話框從建設者?你可以嘗試'alert.create()。show();'?對於第二個問題,嘗試設置'android:minEms =「'只設置它的寬度,它與最小字符數無關,以編程方式檢查 –

+0

對話框將在onCreate方法中創建。顯示但不工作:S。 – iGio90

回答

0

口令的最小長度可以通過使用這樣的圖案...示例

password.getText().toString().matches("[a-zA-Z0-9_]{6,}") 

而關於表示用於錯誤可以使用像吐司輸入的消息來檢查這個例子...

Toast.makeText(this, "Input error!!!", Toast.LENGTH_LONG).show(); 
+0

您好,我在嘗試添加小錯誤彈出窗口之前使用祝酒,是的,他們正在工作,但他們也關閉了對話框!我想保持對話框打開! – iGio90

+0

你有現在你想要什麼???你可以用屬性android:password =「true」創建一個2 EditText的自定義對話框,然後手動設置onClick監聽器的按鈕,並明確地選擇在其中做什麼 – Android

+0

yes!answer posted :) – iGio90

2

嗯,這是我如何解決它進一步的相關問題:

 AlertDialog.Builder alert = new AlertDialog.Builder(this); 
     alert.setCancelable(false); 
     alert.setTitle(getString(R.string.dlgtitle)); 
     alert.setMessage(getString(R.string.dlgmessage)); 
     LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE); 
     LinearLayout layout = (LinearLayout) layoutInflater.inflate(R.layout.pass_create, null); 
     final EditText password = (EditText) layout.findViewById(R.id.password); 
     final EditText passconfirm = (EditText) layout.findViewById(R.id.repeat_password); 
     password.setMinEms(6); 
     alert.setView(layout); 
     alert.setPositiveButton(R.string.ok, null); 
     alert.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int whichButton) { 
       dialog.cancel(); 
      } 
     }); 

     final AlertDialog dialog = alert.create(); 
     dialog.setOnShowListener(new DialogInterface.OnShowListener() { 
      @Override 
      public void onShow(DialogInterface dialogInterface) { 
       Button ok = dialog.getButton(AlertDialog.BUTTON_POSITIVE); 
       if (ok != null) { 
        ok.setOnClickListener(new View.OnClickListener() { 
         @Override 
         public void onClick(View view) { 
          if (!password.getText().toString().equals(passconfirm.getText().toString())) { 
           password.setError(getString(R.string.password_mismatch)); 
          } else if (password.getText().toString().length() <= 6) { 
           password.setError(getString(R.string.pass_lenght)); 
          } else if (password.getText().toString().isEmpty()) { 
           password.setError(getString(R.string.password_empty)); 
          } else { 
           secprefs.putBoolean("first_step", false); 
           secprefs.putString("master_password", password.getText().toString()); 
           dialog.cancel(); 
          } 
         } 
        }); 
       } 
      } 
     }); 
     dialog.show();