2016-04-28 134 views
0

,當我點擊提交按鈕,確認沒有被處理是有沒有搞錯?如果編輯文本爲空,它會進入到下一個活動驗證不被處理?任何一個可以解決這個問題在驗證

public void submitDetails(View v) 
{ 
    et = (EditText)findViewById(R.id.first); 
    et1 = (EditText)findViewById(R.id.last); 
    et2 = (EditText)findViewById(R.id.email); 
    et3 = (EditText)findViewById(R.id.phone); 
    et4 = (EditText)findViewById(R.id.dateofbirth); 
    et5 = (EditText)findViewById(R.id.Address); 
    btn = (Button)findViewById(R.id.submit); 
    String first = et.getText().toString(); 
    String last = et1.getText().toString(); 
    String email = et2.getText().toString(); 
    String mobile = et3.getText().toString(); 
    String birth = et4.getText().toString(); 
    String address = et5.getText().toString(); 
    String emailpatern = "[a-zA-Z0-9._-][email protected][a-z]+\\.+[a-z]+"; 

    if (et.equals("")) 
     et.setError("pls enter name"); 
    else if (et2.equals("")&&!et2.equals(emailpatern)) 
     et2.setError("Pls Enter Valid Email"); 
    else if(et3.equals("")) 
     et3.setError("Enter Mobile Number"); 
    else if (et4.equals("")) 
     et4.setError("Enter Date of Birth"); 
    else if (et5.equals("")) 
     et5.setError("fil the fileds"); 
    else 
     { 
       Intent i = new Intent(MainActivity.this,Result.class); 
       i.putExtra("k1",first); 
       i.putExtra("k2",last); 
       i.putExtra("k3",email); 
       i.putExtra("k4",mobile); 
       i.putExtra("k5",birth); 
       i.putExtra("k6",address); 
       startActivity(i); 
     } 
} 
+0

它看起來像你比較EditText對象本身而不是文本。你有很多變量獲取文本,爲什麼不使用它們?例如,而不是'if(et.equals',使用'if(first.equals'? – Austin

回答

0

您正在檢查EditText的引用是否等於空字符串"",並且這絕不會返回true

替換:

et.equals("") 

通過

et.getText().trim().equals("") 

你必須做你所有的EditText一樣,你可以使用你與EditText上的內容變量..

您的代碼應該看起來像:

if (first.trim().equals("")) 
    et.setError("pls enter name"); 
else if (last.trim().equals("")&&!email.trim().equals(emailpatern)) 
    // ... 
else{ 
    Intent i = new Intent(MainActivity.this,Result.class); 
    i.putExtra("k1",first); 
    i.putExtra("k2",last); 
    i.putExtra("k3",email); 
    i.putExtra("k4",mobile); 
    i.putExtra("k5",birth); 
    i.putExtra("k6",address); 
    startActivity(i); 
}