2017-02-08 39 views
0

我試圖表現出錯誤,當錯誤的值被插入的EditText,下面是我的XML文件EditText上沒有顯示錯誤味精只是顯示錯誤圖標

<EditText 
       android:id="@+id/BasicInfoDOBEditText" 
       android:onClick="onCalenderClick" 
       style="@style/TableRowSearchResultView" 
       android:layout_width="150dp" 
       android:layout_height="wrap_content" 
       android:focusable="false" 
       android:inputType="date" /> 

,以及如何我試圖表現出的錯誤是這樣的。

   dateEditText.setFocusable(true); 
       dateEditText.requestFocus(); 
       dateEditText.setError("wrong input"); 

但它只顯示edittext中沒有「錯誤輸入」的錯誤圖標。我知道有一些解決方案像擴展EditText和重寫setError(),但是有沒有一些簡單的解決方案,或者我正在做錯誤的方式。請幫忙 !

+0

其因爲風格= 「@風格/ TableRowSearchResultView」 行刪除它,如果不需要 –

+0

是必需的。 – FaisalAhmed

+0

@howdoidothis當我們做edittext.setError();錯誤圖標默認顯示 – FaisalAhmed

回答

0

在editText上顯示錯誤我通常更喜歡TextInputLayout。

XML

<android.support.design.widget.TextInputLayout 
    android:layout_margin="@dimen/margin_full" 
    android:id="@+id/input_layout_new_todo" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/toolbar_create_todo"> 

    <EditText 
     android:id="@+id/et_new_todo" 
     android:inputType="text" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="@string/hint_todo_create_new" 
     /> 

</android.support.design.widget.TextInputLayout> 

JAVA

private void getEtData(){ 
    title = todoEt.getText().toString(); 
    if(title.isEmpty() || title == null){ 
     inputLayoutHolder.setErrorEnabled(true); 
     inputLayoutHolder.setError(getResources().getString(R.string.error_no_et_entry)); 
     Handler h = new Handler(); 
     h.postDelayed(new Runnable() { 
      @Override 
      public void run() { 
       inputLayoutHolder.setErrorEnabled(false); 
      } 
     },1500); 
    }else{ 
     title = todoEt.getText().toString(); 
     Log.i(TAG,"todo title: "+title); 
    } 
} 

希望幫助