如何在android中编写EditText错误文本的样式?

时间:2013-01-19 10:52:21

标签: android android-layout

我正在尝试为我的Android应用程序编写新的自定义样式。我需要为在setError中设置EditText后出现的errorText提供样式。

我如何自定义它的风格?

例如:我想在style.xml中设置其background白色和textColor:蓝色等等

enter image description here

7 个答案:

答案 0 :(得分:32)

解决方案即将结束,屏幕截图如下:

Styled error


一些解释

可能 可以使用以下行设置textcolor

yourEditText.setError(Html.fromHtml("<font color='blue'>this is the error</font>"));

但是,这可能无法保证。


根据源代码,显示的Popup类型为ErrorPopup,它是TextView内的内部类。此Popup的内容是从TextView

中膨胀的单个com.android.internal.R.layout.textview_hint
final TextView err = (TextView) inflater.inflate(com.android.internal.R.layout.textview_hint,
      null);

Popup的背景取决于它是否应放在锚点上方:

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

答案 1 :(得分:15)

自从错误弹出uses an internal style

以来,我认为您不能以这种方式自定义其样式
mPopupInlineErrorBackgroundId = getResourceId(mPopupInlineErrorBackgroundId,
                    com.android.internal.R.styleable.Theme_errorMessageBackground);
mView.setBackgroundResource(mPopupInlineErrorBackgroundId);

但是,您可以使用重载的Spanned设置setError(CharSequence, Drawable)和自定义错误图标。

您可以使用fromHtml()从HTML轻松创建Spanned

但是,您仍然无法设置弹出背景图像: - (

答案 2 :(得分:10)

如果编辑文本字段为空,请在表单验证时添加。

            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); 

在编辑文本时,错误标志自动关闭

由于 萨钦

答案 3 :(得分:4)

我已经看到了接受的答案,但我不喜欢拟议的图书馆

我认为这是Android框架中的一个错误,我在这里提交了一个错误: https://code.google.com/p/android/issues/detail?id=158590

编辑: android设计库TextInputLayout小部件可用于在EditText上获得更好的错误处理。

看看它在这里的样子: https://www.youtube.com/watch?v=YnQHb0fNtF8

以及如何在这里实施: http://code.tutsplus.com/tutorials/creating-a-login-screen-using-textinputlayout--cms-24168

答案 4 :(得分:2)

按照此链接获得错误信息的材料设计外观! materialdoc screenshot

答案 5 :(得分:0)

这很好用!

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
}

答案 6 :(得分:0)

我还没有找到解决错误样式的任何方法,但是我创建了带有错误弹出窗口的自定义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() {
        errorPopup?.dismiss()
    }

    private fun showErrorPopup(message: String) {
        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()隐藏错误。

请记住,弹出窗口与活动生命周期相关联,如果您使用多个片段浏览应用程序,请记住在导航时将其关闭。

这是我用于弹出窗口的edit_text_error布局:

<?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>```

If you'd like to move popup to the end of EditText, please change last line in `showErrorPopup()` method to:
`errorPopup?.showAsDropDown(this, 0, 10, Gravity.END)`

You can manipulate the position of popup by modifying gravity or x and y parameters of the `showAsDropDown(view: View, x: Int, y: Int, gravity: Gravity)` method