2011-08-03 40 views
0

*編輯*很抱歉的麻煩,對於錯誤的原因是因爲我不小心給了findViewById(R.id.editTextemail2)到另一個編輯文本,這導致它搞砸了。* EDITED *警報對話框只適用於一個實例(安卓)

我想創建一個警告消息的人誰不輸入正確的電子郵件(不「」或‘@’)和第二消息的人誰不準確鍵入確認電子郵件作爲原版的。有人可以解釋爲什麼我的警報對話框只適用於第一個而不是第二個,即使代碼幾乎相同?警告對話框應該在用戶完成編輯文本後彈出(離開編輯文本焦點)謝謝!

email = (EditText) findViewById(R.id.editTextemail); 
email2 = (EditText) findViewById(R.id.editTextemail2); 
email.setOnFocusChangeListener(new OnFocusChangeListener() { 

@Override 
public void onFocusChange(View arg0, boolean hasFocus) { 
    // checks if it is a proper email 
    if (!hasFocus) { 
     if (!hasPeriod(email.getText().toString()) 
       || !hasAt(email.getText().toString())) { 

      new AlertDialog.Builder(CreateAccount.this) 
        .setTitle("Error") 
        .setMessage(
     "Please enter a properly formatted email address to continue") 
        .setNeutralButton("OK", null).show(); 

     } 

    } 
} 
}); 

email2.setOnFocusChangeListener(new OnFocusChangeListener() { 

@Override 
public void onFocusChange(View arg0, boolean hasFocus) { 
    // checks if it is a proper email 
    if (!hasFocus) { 
     if (!email.getText().toString() 
       .contentEquals(email2.getText().toString())) { 
       new AlertDialog.Builder(CreateAccount.this) 
        .setTitle("Error") 
        .setMessage(
      "Please verify your email addresses match") 
       .setNeutralButton("OK", null).show(); 

     } 

    } 
} 
}); 
+0

?它是否在方法裏面呢?如果是這樣,它在什麼時候失敗?它是否通過Alert Dialog代碼實現,但從未顯示過? –

+0

在eclipse中調試最終將我帶到「未找到源」頁面。所以我只需點擊簡歷,讓程序完成它的工作。但從我所看到的看來,它似乎沒有進入第二種方法。 – Sean

+0

我猜這個顯而易見的問題是'email2'是否會失去焦點?你是否可以把日誌記錄在你的代碼中,看看它是否能夠在該方法中使用它? –

回答

1

我認爲你應該使用等於代替你從調試發現什麼contentEquals

if (!email.getText().toString().equals(email2.getText().toString())) { 
+0

謝謝,但它沒有奏效。看起來我的email2.setOnFOcusChangeListener根本不會被調用,這很奇怪。 – Sean