2012-07-18 35 views
15

Tôi có xác thực cho editText. Nếu trường editText trống thì không thể xác thực và ngăn người dùng chuyển sang một số Activity khác, vì giá trị là bắt buộc. Làm thế nào để làm nó? Tôi biết đây là một câu hỏi cơ bản, nhưng tôi không thể tìm ra cách để làm điều này.editText trước khi chuyển sang Hoạt động khác

Mã của tôi:

btninsert = (Button)findViewById(R.id.btn_insert); 
btninsert.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View v) { 
     insertValues(); 
     EditText userName = (EditText) findViewById(R.id.editText1); 

     if(userName.getText().toString().length() == 0) 
      userName.setError("First name is required!"); 

     Intent i = new Intent(getApplicationContext(), Login.class); 
     startActivity(i); 
    } 
}); 

Khi Button này được nhấp người dùng là chuyển hướng đến màn hình kế tiếp, nhưng tôi cần điều đó nếu xác nhận thất bại họ ở lại trên hiện Activity, và chỉ khi xác nhận thành công (tức là một giá trị đã được nhập) họ đi đến Activity tiếp theo.

Trả lời

39

Thật dễ dàng ... hãy kiểm tra xem EditText của bạn có trống không như trong ví dụ bên dưới.

if(TextUtils.isEmpty(userName.getText())){ 
    /** 
    * You can Toast a message here that the Username is Empty  
    **/ 

    userName.setError("First name is required!"); 

}else{ 
    Intent i = new Intent(getApplicationContext(), Login.class); 
    startActivity(i); 
} 
6

Hãy thử điều này:

bt.setOnClickListener(new OnClickListener() { 
    public void onClick(View arg0) 
    { 
     String str=et.getText().toString(); 

     if(str.equalsIgnoreCase("")) 
     { 
      et.setHint("please enter username");//it gives user to hint 
      et.setError("please enter username");//it gives user to info message //use any one // 
     } 
     else 
     { 
      Intent in=new Intent(getApplicationContext(),second.class); 
      startActivity(in); 
     } 
    } 
}); 
4

Bạn cũng có thể thử điều này:

if ((userName.getText().toString().trim().equals(""))) 
{ 
     Toast.makeText(getApplicationContext(), "User name is empty", Toast.LENGTH_SHORT).show(); 
} 
else 
{ 
     Intent i = new Intent(getApplicationContext(), Login.class); 
     startActivity(i); 
} 
1

Tốt hơn để kiểm tra TextField trống sử dụng Nếu điều kiện cho tất cả các trường hợp xác nhận yêu cầu hoặc đặc biệt và setError tập trung.

If(txtName.getText().toString().trim().equals("")) 
{ 
//Your message or any other validation 
} 
0

Tôi gặp phải sự cố tương tự. Trong trường hợp của tôi, tôi có rất nhiều Edittexts để xác minh, một số là con của một bố cục khác. Mẹo: tất cả các chế độ xem phải ở chế độ xem gốc giống nhau. Vì vậy, để đơn giản:

... 
LinearLayout viewGroup = (LinearLayout) findViewById(R.id.viewGroup); 
checkFieldsRequired(viewGroup); 
... 

public boolean checkFieldsRequired(ViewGroup viewGroup){ 

    int count = viewGroup.getChildCount(); 
    for (int i = 0; i < count; i++) { 
     View view = viewGroup.getChildAt(i); 
     if (view instanceof ViewGroup) 
      checkFieldsRequired((ViewGroup) view); 
     else if (view instanceof EditText) { 
      EditText edittext = (EditText) view; 
      if (edittext.getText().toString().trim().equals("")) {     
       edittext.setError("Required!");     
      } 
     } 
    } 

    return false; 
} 
1

Tôi hiểu đó là một câu hỏi cũ nhưng có thể điều này giúp, bạn có thể sử dụng .isEmpty() thay vì Equals()

if(userName.getText().toString().isEmpty()){ 

    /** 
    * You can Toast a message here that the Username is Empty  
    **/ 

    userName.setError("First name is required!"); 

}else{ 
    Intent i = new Intent(getApplicationContext(), Login.class); 
    startActivity(i); 
} 
Các vấn đề liên quan