2013-08-14 22 views
20

Tôi muốn hiển thị lỗi nếu người dùng nhập giá trị trống vào edittext.But tôi không nhận được cách tôi có thể thực hiện việc này. Đây là cách tôi muốn như sau:Hiển thị Lỗi trên đầu của Chỉnh sửa Văn bản Android

enter image description here

Đây là xml của tôi mà tôi đã tạo ra

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 

    <LinearLayout 
     android:id="@+id/headerLayout" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:background="@drawable/top_bg" 
     android:orientation="horizontal" > 

     <ImageView 
      android:id="@+id/back_button" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="10dp" 
      android:layout_marginTop="5dp" 
      android:src="@drawable/back_button" /> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="75dp" 
      android:layout_marginTop="10dp" 
      android:text="Traveller Details" 
      android:textColor="@android:color/white" /> 
    </LinearLayout> 
    <LinearLayout 
     android:id="@+id/tittleLayout" 
     android:layout_below="@+id/headerLayout" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/TittleTravellerDetails" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_marginLeft="10dp" 
      android:layout_marginTop="5dp" 
      android:gravity="left" 
      android:text="Traveller Details" /> 

     <View 
      android:layout_width="wrap_content" 
      android:layout_height="2dip" 
      android:layout_marginTop="2dp" 
      android:background="#FF909090" /> 

    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/passengerDetails" 
     android:layout_below="@+id/tittleLayout" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:orientation="vertical"> 

     <Spinner 
      android:id="@+id/Tittle" 
      android:layout_width="290dp" 
      android:layout_marginLeft="5dp" 
      android:layout_marginTop="5dp" 
      android:layout_height="wrap_content"/> 
     <EditText 
      android:id="@+id/firstName" 
      android:layout_width="290dp" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="5dp" 
      android:layout_marginTop="5dp" 
      android:hint="First Name" /> 

     <EditText 
      android:id="@+id/LastName" 
      android:layout_width="290dp" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="5dp" 
      android:layout_marginTop="5dp" 
      android:hint="Last Name" /> 

    </LinearLayout> 
    <LinearLayout 
     android:id="@+id/ContactDetailsLayout" 
     android:layout_below="@+id/passengerDetails" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:layout_marginTop="10dp" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/TittleContactDetails" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_marginLeft="10dp" 
      android:layout_marginTop="5dp" 
      android:gravity="left" 
      android:text="ContactDetails" /> 

     <View 
      android:layout_width="wrap_content" 
      android:layout_height="2dip" 
      android:layout_marginTop="2dp" 
      android:background="#FF909090" /> 

    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/mobileEmailDetails" 
     android:layout_below="@+id/ContactDetailsLayout" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:orientation="vertical"> 

     <EditText 
      android:id="@+id/mobileNumber" 
      android:layout_width="290dp" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="5dp" 
      android:layout_marginTop="5dp" 
      android:inputType="number" 
      android:hint="Mobile No" /> 

     <TextView 
      android:id="@+id/emailid" 
      android:layout_width="284dp" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="8dp" 
      android:layout_marginTop="5dp" 
      android:hint="Email ID" /> 

    </LinearLayout> 
    <LinearLayout 
     android:id="@+id/continueBooking" 
     android:layout_below="@+id/mobileEmailDetails" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:orientation="vertical"> 

     <ImageView 
      android:id="@+id/continuebooking" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_marginLeft="20dp" 
      android:layout_marginTop="25dp" 

      android:src="@drawable/continue" /> 

    </LinearLayout> 

</RelativeLayout> 

Hoạt động Mã

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.activity_main); 
     emailId = (TextView)findViewById(R.id.emailid); 
     continuebooking = (ImageView)findViewById(R.id.continuebooking); 
     firstName= (EditText)findViewById(R.id.firstName); 
     emailId.setText("[email protected]"); 
     setTittle(); 
     continuebooking.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 

       if(firstName.getText().toString().trim().equalsIgnoreCase("")){ 
        firstName.setError("Enter FirstName"); 
       } 

      } 
     }); 
    } 

Vì vậy, nếu người dùng không nhập tên của anh ấy, tôi muốn hiển thị lỗi như hình ảnh mà bạn chưa nhập bất kỳ tên nào như vậy.Xin vui lòng giúp tôi tôi mới trong android.Thanks

+0

Vì vậy, bạn muốn di chuyển đến cuối nhọn của bong bóng một số pixel bên trái? – ZooMagic

Trả lời

1

Với youredittext.equals("") bạn có thể biết nếu người dùng chưa nhập bất kỳ chữ cái nào.

+0

tôi biết điều này tôi muốn cho thấy rằng lỗi trên đầu của văn bản chỉnh sửa. Vui lòng xem hình ảnh để tham khảo – Developer

68

Bạn có thể hiển thị lỗi như PopUp của EditText

if (editText.getText().toString().trim().equalsIgnoreCase("")) { 
     editText.setError("This field can not be blank"); 
} 

và điều đó sẽ được trông giống như sau

enter image description here

firstName.addTextChangedListener(new TextWatcher() { 

    @Override 
    public void onTextChanged(CharSequence s, int start, int before, int count) { 
    } 

    @Override 
    public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
    } 

    @Override 
    public void afterTextChanged(Editable s) { 
     if (firstName.getText().toString().length <= 0) { 
      firstName.setError("Enter FirstName"); 
     } else { 
      firstName.setError(null); 
     } 
    } 
}); 
+0

tôi biết điều này tôi muốn hiển thị lỗi trên đầu của văn bản chỉnh sửa. Vui lòng xem hình ảnh để tham khảo – Developer

+0

@Gaurav tôi đã đưa ra những điều tương tự như những gì bạn muốn. thử này – SilentKiller

+0

nhưng một lần nữa nếu tôi nhập một số giá trị lỗi này là không loại bỏ – Developer

0

bạn có thể sử dụng một sự kiện onchange để kích hoạt một xác nhận và sau đó bạn có thể hiển thị bánh mì nướng nếu điều này là đủ cho bạn

18
private void showError() { 
    mEditText.setError("Password and username didn't match"); 
} 

nào sẽ gây ra lỗi thể hiện như thế này:

enter image description here

Và nếu bạn muốn loại bỏ nó:

textView.setError(null); 
+1

sau khi văn bản thay đổi cách chúng tôi xóa lỗi này – Developer

+1

textView.setError (null); –

0

Bạn đã viết mã của bạn trong onClick sự kiện. Điều này sẽ gọi khi bạn nhấp vào EditText. Nhưng đây là một cái gì đó giống như bạn đang kiểm tra nó trước khi vào.

Vì vậy, đề xuất của tôi là gì, bạn nên sử dụng focus changed. Khi bất kỳ chế độ xem nào lấy tiêu điểm, bạn đang đặt no error và khi tiêu điểm thay đổi, bạn kiểm tra xem có đầu vào hợp lệ hay không như dưới đây.

firstName.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
    @Override 
    public void onFocusChange(View arg0, boolean arg1) { 
     firstName.setError(null); 
     if (firstName.getText().toString().trim().equalsIgnoreCase("")) { 
      firstName.setError("Enter FirstName"); 
     } 
    } 
}); 
+0

i am không nhận được như thế nào tôi sẽ sử dụng này ?? – Developer

+1

@Gaurav, tôi cũng không triển khai dự án này. Chờ đã .. sẽ kiểm tra sớm thôi .. –

+0

@Gaurav, bạn có thể cho tôi biết bạn gặp phải vấn đề chính xác nào không? Nếu không vấn đề đầu tiên được trả lời là đủ tốt để làm như vậy .. –

3

tôi có các giải pháp của vấn đề của tôi Hy vọng điều này sẽ giúp những người khác đã sử dụng also.I onTextChnaged nó gọi Khi một đối tượng của một loại được gắn vào một Editable, phương pháp của nó sẽ được gọi khi văn bản được thay đổi.

public class MainActivity extends Activity { 
    TextView emailId; 
    ImageView continuebooking; 
    EditText firstName; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.activity_main); 
     emailId = (TextView)findViewById(R.id.emailid); 
     continuebooking = (ImageView)findViewById(R.id.continuebooking); 
     firstName= (EditText)findViewById(R.id.firstName); 
     emailId.setText("[email protected]"); 
     setTittle(); 
     continuebooking.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 

       if(firstName.getText().toString().trim().equalsIgnoreCase("")){ 
        firstName.setError("Enter FirstName"); 
       } 

      } 
     }); 
     firstName.addTextChangedListener(new TextWatcher() { 

      @Override 
      public void onTextChanged(CharSequence s, int start, int before, int count) { 
       firstName.setError(null); 

      } 

      @Override 
      public void beforeTextChanged(CharSequence s, int start, int count, 
        int after) { 
       // TODO Auto-generated method stub 

      } 

      @Override 
      public void afterTextChanged(Editable s) { 
       firstName.setError(null); 

      } 
     }); 
    } 
0

Có vẻ như tất cả những gì bạn không thể làm là hiển thị lỗi ở cuối editText. Đặt chiều rộng editText của bạn để khớp với chiều rộng của bố cục gốc bao trùm. Sẽ hoạt động tốt.

2
if(TextUtils.isEmpty(firstName.getText().toString()){ 
     firstName.setError("TEXT ERROR HERE"); 
} 

Hoặc bạn cũng có thể sử dụng TextInputLayout trong đó có một số phương pháp hữu ích và một số người dùng hoạt hình thân thiện

Các vấn đề liên quan