2015-12-23 42 views
6

我有一個TextInputLayout裏面有一個EditText。TextInputLayout錯誤顏色沒有清除?

這是我的xml:

<android.support.design.widget.TextInputLayout 
    android:id="@+id/textInputLayout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <EditText 
     android:id="@+id/editText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:hint="Enter Text" /> 

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

我的Java代碼:

((TextInputLayout) findViewById(R.id.textInputLayout)).setError("ERROR"); 

當我打電話SETERROR( 「ERROR」),在和標籤(提示)顏色和EditText上的底部線顏色變爲紅色並且出現錯誤。這是我期望的行爲。

現在我們假設在銷燬我的活動之前,我不會撥打setError(null)。現在我再次打開相同的活動。我可以看到,我的應用程序中的所有EditText字段的底線仍爲紅色,儘管標籤顏色似乎已被重置並且錯誤消息已被解除。這並不總是可重複的,但如果我繼續嘗試,我終究可以得到它。

我在5.1.1中使用Nexus 4。

我做錯了什麼?

+0

它應該重新創建佈局,我不明白爲什麼即使在銷燬活動後你也會變紅線。你可以發佈多一點的代碼? – Mangesh

+0

嘿,你有沒有解決這個問題。我的也一樣 –

回答

5

這是由於AppCompat庫中的錯誤引起的。

通過elyess.a ... @ gmail.com報道,2015年10月19日使用的設計支持 庫23.1.0

步驟來重現問題(包括示例代碼)。

  • (在形式即)上與一個TIL SETERROR
  • 的TIL具有紅色下劃線(OK)
  • 導航返回並再次進入活動。或者去TIL的另一個活動。

發生了什麼事。

  • 即使在其他活動中,所有TIL都有紅色下劃線。 (但沒有錯誤文本)。
  • 只有在關閉應用程序後,紅色下劃線纔會消失。

這裏也有報道:


問題狀態改爲FutureRelease於2015年11月11日,所以希望對大家能修復即將推出。

同時,似乎有3個解決方法:

0

這個問題上com.android.support的23.1.1版本解決:...庫

0

正如@Richard所說,這是一個錯誤。 Issue 190829: TextInputLayout setError causes all TILs in the app to have red underline

我已經使用了將常數狀態設置回背景的解決方案。你可以只用自己的自定義類擴展TextInputLayout,你重寫SETERROR()方法:

public class CustomTextInputLayout extends TextInputLayout { 

    // Constructors... 

    @Override 
    public void setError(@Nullable CharSequence error) { 

     super.setError(error); 
     if ((getEditText() != null && getEditText().getBackground() != null) && 
      (Build.VERSION.SDK_INT == 22 || Build.VERSION.SDK_INT == 21)) { 
      Drawable drawable = getEditText().getBackground().getConstantState().newDrawable(); 
      getEditText().setBackgroundDrawable(drawable); 
     } 
    } 
} 

然後我再用這個類包裝EditTexts。我沒有遇到任何副作用。