我正在尝试为我的Android应用程序编写新的自定义样式。 我需要为在EditText
中设置setError
后出现的错误文本(errorText)提供样式。
如何自定义它的样式?
例如:我想在style.xml中将其background
设置为白色,textColor
设置为蓝色等等。
我正在尝试为我的Android应用程序编写新的自定义样式。 我需要为在EditText
中设置setError
后出现的错误文本(errorText)提供样式。
如何自定义它的样式?
例如:我想在style.xml中将其background
设置为白色,textColor
设置为蓝色等等。
一些解释
您可能可以使用以下行设置文本颜色:
yourEditText.setError(Html.fromHtml("<font color='blue'>this is the error</font>"));
然而,这可能不能得到保证。
根据源代码,显示的此 Popup
是 TextView
内部类 ErrorPopup
的一种类型。该 Popup
的内容是从 com.android.internal.R.layout.textview_hint
膨胀而来的单个 TextView
。
final TextView err = (TextView) inflater.inflate(com.android.internal.R.layout.textview_hint,
null);
这个弹出框
的背景取决于它是否应该放置在锚点上方:
if (above) {
mView.setBackgroundResource(com.android.internal.R.drawable.popup_inline_error_above);
} else {
mView.setBackgroundResource(com.android.internal.R.drawable.popup_inline_error);
}
由于创建弹出窗口所使用的所有Android资源都是内部的,并最终硬编码的,因此您最好自己创建错误弹出窗口。这很容易,而且您实际上不会干扰正常的EditText
,因为默认弹出窗口仅用于显示错误,因此创建自己的弹出窗口是可以接受的。
解决方案
我在这里创建了它:WidgyWidgets
我认为你无法以那种方式自定义其样式,因为错误弹出窗口使用内部样式:
mPopupInlineErrorBackgroundId = getResourceId(mPopupInlineErrorBackgroundId,
com.android.internal.R.styleable.Theme_errorMessageBackground);
mView.setBackgroundResource(mPopupInlineErrorBackgroundId);
然而,你可以使用重载的 setError(CharSequence, Drawable)
方法设置一个 Spanned
和自定义的错误图标。
你可以使用 fromHtml()
轻松地从 HTML 创建一个 Spanned
。
然而,你仍然无法设置弹出式背景图片 :-(
int ecolor = R.color.black; // whatever color you want
String estring = "Please enter a valid email address";
ForegroundColorSpan fgcspan = new ForegroundColorSpan(ecolor);
SpannableStringBuilder ssbuilder = new SpannableStringBuilder(estring);
ssbuilder.setSpan(fgcspan, 0, estring.length(), 0);
edtEmail.requestFocus();
edtEmail.setError(ssbuilder);
当你在编辑文本时,错误标记会自动消失。
谢谢 Sachin
请点击以下链接,查看一个漂亮的错误信息的 Material Design 样式!
materialdoc
我发现链接已经失效,这里提供新的方法:使用 TextInputLayout
。
<android.support.design.widget.TextInputLayout
android:id="@+id/ipAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="-30dp"
android:layout_gravity="end"
android:digits="1234567890"
app:errorEnabled="true" >
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:maxLines="1"
android:width="200dp"
android:text="@{ethernetData.ipAddress}"
android:enabled="@{!ethernetData.ethernetAutoIp}"/>
在TextWatcher
中的代码(我使用数据绑定),在您的TextInputLayout
上调用setError
方法。
binding.ipAddress.setError(your error);
binding.ipAddress.getEditText().addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
if(! Patterns.IP_ADDRESS.matcher(editable.toString()).matches()) {
binding.ipAddress.setError(your error string);
IpAddressOK = false;
}
else{
binding.ipAddress.setError(null);
IpAddressOK = true;
}
}
});
这个很好用!
private fun setErrorOnSearchView(searchView: SearchView, errorMessage:
String) {
val id = searchView.context
.resources
.getIdentifier("android:id/search_src_text", null, null)
val editText = searchView.find<EditText>(id)
val errorColor = ContextCompat.getColor(this,R.color.red)
val fgcspan = ForegroundColorSpan(errorColor)
val builder = SpannableStringBuilder(errorMessage)
builder.setSpan(fgcspan, 0, errorMessage.length, 0)
editText.error = builder
}
我还没有找到任何编辑错误样式的解决方案,但是我已经创建了带有错误弹出窗口的自定义EditText。希望这能有所帮助:
import android.content.Context
import android.util.AttributeSet
import android.view.Gravity
import android.view.LayoutInflater
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.PopupWindow
import android.widget.TextView
import androidx.annotation.StringRes
class ErrorEditText : EditText {
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
private var errorPopup: PopupWindow? = null
fun showError(message: String) {
showErrorPopup(message)
}
fun showError(@StringRes messageResId: Int) {
showErrorPopup(context.getString(messageResId))
}
fun dismissError() {
post {errorPopup?.dismiss() }
}
private fun showErrorPopup(message: String) {
post {
dismissError()
val inflater = (context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater)
val view = inflater.inflate(R.layout.edit_text_error, null, false)
errorPopup =
PopupWindow(view, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)
errorPopup?.contentView?.findViewById<TextView>(R.id.message)?.text = message
errorPopup?.showAsDropDown(this, 0, 10, Gravity.START)
}
}
}
showError(message: String)
或 showError(@StringRes messageResId: String)
,并使用方法 dismissError()
隐藏它。<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:elevation="2dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:background="#fff">
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#E20000"
android:padding="8dp"
android:textSize="11sp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:text="Please enter a valid email address!" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
showErrorPopup()
方法中的最后一行为:errorPopup?.showAsDropDown(this, 0, 10, Gravity.END)
您可以通过修改showAsDropDown(view: View, x: Int, y: Int, gravity: Gravity)
方法的gravity或x和y参数来操纵弹出窗口的位置。Unable to start activity ComponentInfo{package.name.views.MainActivity}: android.view.WindowManager$BadTokenException: Unable to add window -- token null
- OhhhThatVarunrunOnUiThread { showErrorPopup("Message") }
。 - Milszym