如何更改可以设置为显示在文本字段下方的错误消息的颜色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>
Run Code Online (Sandbox Code Playgroud)
和错误外观(设置为绿色进行测试):
<style name="WelcomeErrorAppearance" parent="@android:style/TextAppearance">
<item name="android:textColor">@android:color/holo_green_dark</item>
</style>
Run Code Online (Sandbox Code Playgroud)
结果是提示以及错误消息被着色(来自缩放的Android模拟器的屏幕截图):
常规(无错误):
错误状态:
编辑2 /结果:
出现错误消息时,字段上方的提示将更改为与错误消息相同的颜色,覆盖提示颜色 - 这是设计使然.
我正在尝试创建一个仅在应用程序第一次启动时出现的弹出窗口.我希望它显示一些文本并有一个按钮来关闭弹出窗口.但是,我在让PopupWindow工作时遇到了麻烦.我尝试了两种不同的方法:
首先我有一个XML文件,它声明了popup.xml的布局(在linearlayout中的textview),我在主Activity的OnCreate()中添加了这个:
PopupWindow pw = new PopupWindow(findViewById(R.id.popup), 100, 100, true);
pw.showAtLocation(findViewById(R.id.main), Gravity.CENTER, 0, 0);
Run Code Online (Sandbox Code Playgroud)
其次,我使用此代码完全相同:
final LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
PopupWindow pw = new PopupWindow(inflater.inflate(R.layout.popup, (ViewGroup) findViewById(R.layout.main) ), 100, 100, true);
pw.showAtLocation(findViewById(R.id.main_page_layout), Gravity.CENTER, 0, 0);
Run Code Online (Sandbox Code Playgroud)
第一个抛出NullPointerException,第二个抛出BadTokenException并说"无法添加窗口 - 令牌null无效"
世界上我做错了什么?我是非常新手所以请耐心等待.
我想要做的是更改使用setError()方法后显示的弹出错误消息的背景颜色(设置自定义drawable).
目前,它看起来像这样:

我发现Android有两个文件:
popup_inline_error.9.pngpopup_inline_above_error.9.png你应该能够使用两个属性设置它们:
errorMessageBackgrounderrorMessageAboveBackground但是当我尝试在我的主题中设置它们时,我得到的只是:
<item name="errorMessageBackground">@drawable/popup_inline_error_holo_light</item>
<item name="errorMessageAboveBackground">@drawable/popup_inline_error_above_holo_light</item>
error: Error: No resource found that matches the given name: attr 'errorMessageBackground'.
Run Code Online (Sandbox Code Playgroud)
(它是一样的with android:errorMessageBackground)
我在这里提出这个问题,因为我的想法已经用完了 - 也许有人已经设法做到了这一点?
编辑: 我正在使用的主题的标题:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style
name="Theme.MyThemeName"
parent="@style/Theme.Sherlock.Light">
Run Code Online (Sandbox Code Playgroud)
另一个编辑: 呃,我发现我的问题是一个副本: android:errorMessageBackground在styles.xml中找不到资源发现错误
是另一个编辑:这是一个已知问题,请看一下这个链接:https://code.google.com/p/android/issues/detail?id = 55879
在我的表格中,我setError("")在EditText场上使用.我的应用程序主题扩展android:Theme.Holo.
我已经手动用深色背景设置图像android:errorMessageBackground和android:errorMessageBackgroundAbove.
现在问题是:错误消息的文本颜色也很暗,不可读.
我尝试textColor在我的主题中更改不同的属性,但我找不到正确的属性.
请问有人能帮助我吗?谢谢!克里斯
我想为我设置一个setError方法TextView,使用自定义图标而不是默认的Android图标.所以我尝试了这个:
((EditText)findViewById(R.id.edtTitle)).setError(getResources().getText(R.string.errEmptyTitle),
getResources().getDrawable(R.drawable.ico_warning_small);
Run Code Online (Sandbox Code Playgroud)
它向我显示自定义消息,但不显示自定义图标.我也尝试过这个:
Drawable warning = (Drawable)getResources().getDrawable(R.drawable.ico_warning_small);
((EditText)findViewById(R.id.edtTitle))
.setError(getResources().getText(R.string.errEmptyTitle), warning);
Run Code Online (Sandbox Code Playgroud)
几乎一样,但我仍然决定试一试.然而这也没有帮助 - 我仍然看不到图标.我尝试使用其他一些Android系统图标,只是为了看看我是否看到它们,我也看不到它们.
那么我做错了什么?有没有办法设置自定义图标?
当我edittext.setError("enter a comment")在android中使用时,它工作正常,直到键盘建议出现并且错误被推到上面edittext,之后它不显示整个错误消息.
它为什么这样做?

有没有办法在TextView中为错误弹出窗口设置自定义布局:
.
在textView.setError(String, Drawable)我们只能设定一个错误图标.
类中的showError()方法TextView and ErrorPopup是私有的,所以我无法使用它们.
有任何想法吗?提前致谢!迈克尔
UPD:
感谢您的评论,但据我了解,主题技巧在这里不适用:(来自android-10的TextView 3384行)
void fixDirection(boolean above) {
mAbove = above;
if (above) {
mView.setBackgroundResource(com.android.internal.R.drawable.popup_inline_error_above);
} else {
mView.setBackgroundResource(com.android.internal.R.drawable.popup_inline_error);
}
}
Run Code Online (Sandbox Code Playgroud)
在android-15中使用com.android.internal.R.styleable.Theme_errorMessageBackground主题参数,但它是内部的.
因此,我没有看到任何解决此问题的方法,除了编写我自己的错误通知器):
我知道我们可以使用改变编辑文本字体Typeface.但是我们为编辑文本设置的错误呢?看下面的代码:
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/ATaha.ttf");
private EditText mPasswordView;
mPasswordView = (EditText) findViewById(R.id.password);
mPasswordView.setTypeface(font);
Run Code Online (Sandbox Code Playgroud)
使用此代码我只能更改编辑文本字体但是当我设置这样的错误时:
mPasswordView.setError(getString(R.string.error_field_required));
Run Code Online (Sandbox Code Playgroud)
错误通知字体是android默认字体,并且不使用类型face更改.我怎么能改变呢?
我想更改EditText setError().
我试过这个:
editText.setError(Html.fromHtml("<span style=\"font-family: sans-serif !important\">hello</span>"));
Run Code Online (Sandbox Code Playgroud)
当用户选择另一种字体时,我想更改三星手机中错误消息的字体系列。
任何想法将不胜感激。
提前致谢
我的xml代码的密码字段部分:
<android.support.percent.PercentRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
...
<android.support.design.widget.TextInputLayout
android:layout_below="@+id/uname_ly"
android:id="@+id/text_input_layout_passwd"
app:layout_widthPercent="70%"
android:layout_centerHorizontal="true"
app:layout_heightPercent="10%"
app:layout_marginTopPercent="0%"
app:layout_marginBottomPercent="0%"
android:adjustViewBounds="true"
android:textColorHint="@color/editTextHintColor"
app:hintTextAppearance="@style/TextAppearance.App.TextInputLayout"
>
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/nopasswd"
android:inputType="textPassword"
android:maxLines="1"
android:textColor="@color/editTextTextColor" />
</android.support.design.widget.TextInputLayout>
...
Run Code Online (Sandbox Code Playgroud)
[更新样式]
<style name="TextAppearance.App.TextInputLayout" parent="@android:style/TextAppearance">
<item name="android:textColor">@color/editTextHintColor</item>
</style>
Run Code Online (Sandbox Code Playgroud) android android-layout android-edittext android-styles android-textinputlayout