2016-04-28 139 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); 
}