TextInputLayout 오류 메시지 색상을 설정하는 방법은 무엇입니까?
텍스트 필드 아래에 표시되도록 설정할 수있는 오류 메시지의 색상을 어떻게 변경할 수 있습니까 TextInputLayout
(경유 setError(...)
– 여기에서 오류 상태 참조 )?
일반적으로 빨간색으로 표시되며 변경하고 싶습니다. styles.xml
색상을 지정하려면 파일 에서 어떤 항목 이름 / 키를 사용해야 합니까?
미리 감사드립니다.
편집하다:
app:errorTextAppearance
내 키 추가 TextInputLayout
:
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:id="@+id/welcome_current_week_container"
app:errorTextAppearance="@style/WelcomeErrorAppearance">
<EditText
..../>
</android.support.design.widget.TextInputLayout>
</LinearLayout>
및 오류 모양 (테스트를 위해 녹색으로 설정) :
<style name="WelcomeErrorAppearance" parent="@android:style/TextAppearance">
<item name="android:textColor">@android:color/holo_green_dark</item>
</style>
그 결과 힌트와 오류 메시지가 색상이 지정됩니다 (확장 된 Android 에뮬레이터의 스크린 샷) .
일반 (오류 없음) :
오류 상태 :
2 / 결과 수정 :
오류 메시지가 나타나면 필드 위의 힌트가 오류 메시지와 동일한 색상으로 변경되어 힌트 색상을 무시합니다. 이것은 의도적으로 설계된 것입니다.
파일 @android:style/TextAppearance
에서 상위로 사용하는 사용자 정의 스타일을 만듭니다 styles.xml
.
<style name="error_appearance" parent="@android:style/TextAppearance">
<item name="android:textColor">@color/red_500</item>
<item name="android:textSize">12sp</item>
</style>
그리고 TextInputLayout 위젯에서 사용하십시오.
<android.support.design.widget.TextInputLayout
android:id="@+id/emailInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:errorTextAppearance="@style/error_appearance">
편집 : 당신의 TextInputLayout (내부 객체에 힌트 설정 EditText
, TextView
힌트 및 오류에 대해 서로 다른 색상을 유지하는 등).
사실, 바로 오류 메시지 색상을 변경하려면 설정할 수 있습니다 textColorError
테마에 (도 설정 colorControlNormal
및 colorControlActivated
일반 위젯과 힌트 텍스트의 색상). TextInputLayout
그 속성을 선택합니다. 참고 :errorTextAppearance
사용자 지정 스타일로 설정 하면 textColorError
효과가 없습니다.
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorControlNormal">@color/control_normal</item>
<item name="colorControlActivated">@color/control_activated</item>
<item name="textColorError">@color/error</item>
<!-- other styles... -->
</style>
그리고 AndroidManifest.xml에서 :
<application
android:theme="@style/AppTheme"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<!-- ... -->
</application>
나는 이것을 동적으로해야했다. 반사 사용 :
public static void setErrorTextColor(TextInputLayout textInputLayout, int color) {
try {
Field fErrorView = TextInputLayout.class.getDeclaredField("mErrorView");
fErrorView.setAccessible(true);
TextView mErrorView = (TextView) fErrorView.get(textInputLayout);
Field fCurTextColor = TextView.class.getDeclaredField("mCurTextColor");
fCurTextColor.setAccessible(true);
fCurTextColor.set(mErrorView, color);
} catch (Exception e) {
e.printStackTrace();
}
}
textInputLayout.setErrorEnabled(true)
이 작업을 수행하려면 위의 메서드 를 호출 하기 전에 호출 해야합니다 .
한 쪽 참고. 나는 errorTextAppereance
. 정말 잘 작동하지만 처음에는 새 errorTextAppereance
스타일 을 적용한 후 입력 밑줄 색상이 변경되지 않았습니다 . 몇 가지 의견이 있으며 다른 사람들도 동일한 문제를 겪고 있음을 확인했습니다.
제 경우에는 새 오류 텍스트를 설정 한 후 새 스타일을 설정할 때 이런 일이 발생했습니다. 이렇게 :
passwordInputLayout.error = "Password strength"
passwordInputLayout.setErrorTextAppearance(R.style.InputError_Purple)
이 두 가지 방법의 순서를 전환하면 텍스트와 밑줄 색상이 예상대로 변경됩니다.
passwordInputLayout.setErrorTextAppearance(R.style.InputError_Purple)
passwordInputLayout.error = "Password strength"
그리고 오류 텍스트 모양 스타일은 다음과 같습니다.
<style name="InputError" parent="TextAppearance.Design.Error"/>
<style name="InputError.Purple">
<item name="android:textColor">@color/purple</item>
</style>
최신 정보
대신 사용자 정의보기를 사용하십시오.
내 경우에서 작동하는 @ jared 's Answer의 수정 버전 :
public static void setErrorTextColor(TextInputLayout textInputLayout, int color) {
try {
Field fErrorView = TextInputLayout.class.getDeclaredField("mErrorView");
fErrorView.setAccessible(true);
TextView mErrorView = (TextView)fErrorView.get(textInputLayout);
mErrorView.setTextColor(color);
mErrorView.requestLayout();
} catch (Exception e) {
e.printStackTrace();
}
}
If you are using com.google.android.material.textfield.TextInputLayout this input layout than you just set one style
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textInputLayoutPassword"
style="@style/LoginTextInputLayoutStyle"
<style name="LoginTextInputLayoutStyle" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense">
<item name="boxStrokeColor">@color/text_input_box</item>
<item name="errorTextColor">@color/colorRed</item>
</style>
Depending on need, one can change/set TextInputLayout text color dynamically or directly in the layout XML file. Below is sample code snippets
Create a custom style which uses @android:style/TextAppearance as parent in your styles.xml file:
<style name="style_error_appearance" parent="@android:style/TextAppearance">
<item name="android:textColor">@color/color_error</item>
<item name="android:textSize">11sp</item>
</style>
And, use it in your TextInputLayout widget:
- Directly in XML Layout
<android.support.design.widget.TextInputLayout
android:id="@+id/your_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:errorTextAppearance="@style/style_error_appearance">
- Dynamically in your class
your_input_layout.setErrorTextAppearance(R.style.style_error_appearance);
If you want to set single/same error text color for your application then define the text color in your app theme
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Error text color... -->
<item name="textColorError">@color/color_error</item>
<!-- other styles... -->
</style>
And in your AndroidManifest.xml:
<application
android:theme="@style/AppTheme"
android:icon="@drawable/ic_launcher"
android:label="@string/your_app_name">
<!-- ... -->
</application>
I looked into the TextInputLayout source and I realised that error text color is gotten from colors.xml. Just add this to your colors.xml:
<color name="design_textinput_error_color_light" tools:override="true">your hex color</color>
참고 URL : https://stackoverflow.com/questions/33709066/how-to-set-textinputlayout-error-message-colour
'developer tip' 카테고리의 다른 글
moment.js isValid 함수가 제대로 작동하지 않습니다. (0) | 2020.10.26 |
---|---|
컨트롤러별로 다른 컨트롤러의 부분보기를 반환하는 방법은 무엇입니까? (0) | 2020.10.26 |
TensorFlow 변수에 값을 할당하는 방법은 무엇입니까? (0) | 2020.10.26 |
SYSTEM_ALERT_WINDOW-Android 6.0 및 targetSdkVersion 23에서이 권한을 자동으로 얻는 방법 (0) | 2020.10.26 |
ReSharper 오류 :“출력이 한도에 도달하여 잘 렸습니다. (0) | 2020.10.26 |