Fra*_*ine 35 android android-layout android-design-library androiddesignsupport android-textinputlayout
TextInputLayout包含一个EditText,后者又接收来自用户的输入.使用Android设计支持库中引入的TextInputLayout,我们应该将错误设置为持有EditText而不是EditText本身的TextInputLayout.编写UI时,将只关注EditText而不是整个TextInputLayout,这可能导致键盘覆盖错误.在以下GIF通知中,用户必须先删除键盘才能看到错误消息.这与设置IME动作以使用键盘继续前进相结合会导致真正令人困惑的结果.
布局xml代码:
<android.support.design.widget.TextInputLayout
android:id="@+id/uid_text_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:errorEnabled="true"
android:layout_marginTop="8dp">
<EditText
android:id="@+id/uid_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:hint="Cardnumber"
android:imeOptions="actionDone"/>
</android.support.design.widget.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)
Java代码将错误设置为TextInputLayout:
uidTextInputLayout.setError("Incorrect cardnumber");
Run Code Online (Sandbox Code Playgroud)
如何在用户没有看到错误消息的情况下确保错误消息可见?有可能移动焦点吗?
为了确保错误消息在用户没有看到它的情况下可见,我TextInputLayout将其子类化并放入一个ScrollView.这使我可以在需要时向下滚动以显示错误消息,在每次设置错误消息时.使用它的activity/fragment类中不需要进行任何更改.
import androidx.core.view.postDelayed
/**
* [TextInputLayout] subclass that handles error messages properly.
*/
class SmartTextInputLayout @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : TextInputLayout(context, attrs, defStyleAttr) {
private val scrollView by lazy(LazyThreadSafetyMode.NONE) {
findParentOfType<ScrollView>() ?: findParentOfType<NestedScrollView>()
}
private fun scrollIfNeeded() {
// Wait a bit (like 10 frames) for other UI changes to happen
scrollView?.postDelayed(160) {
scrollView?.scrollDownTo(this)
}
}
override fun setError(value: CharSequence?) {
val changed = error != value
super.setError(value)
// work around https://stackoverflow.com/q/34242902/1916449
if (value == null) isErrorEnabled = false
// work around https://stackoverflow.com/q/31047449/1916449
if (changed) scrollIfNeeded()
}
}
Run Code Online (Sandbox Code Playgroud)
以下是帮助方法:
/**
* Find the closest ancestor of the given type.
*/
inline fun <reified T> View.findParentOfType(): T? {
var p = parent
while (p != null && p !is T) p = p.parent
return p as T?
}
/**
* Scroll down the minimum needed amount to show [descendant] in full. More
* precisely, reveal its bottom.
*/
fun ViewGroup.scrollDownTo(descendant: View) {
// Could use smoothScrollBy, but it sometimes over-scrolled a lot
howFarDownIs(descendant)?.let { scrollBy(0, it) }
}
/**
* Calculate how many pixels below the visible portion of this [ViewGroup] is the
* bottom of [descendant].
*
* In other words, how much you need to scroll down, to make [descendant]'s bottom
* visible.
*/
fun ViewGroup.howFarDownIs(descendant: View): Int? {
val bottom = Rect().also {
// See https://stackoverflow.com/a/36740277/1916449
descendant.getDrawingRect(it)
offsetDescendantRectToMyCoords(descendant, it)
}.bottom
return (bottom - height - scrollY).takeIf { it > 0 }
}
Run Code Online (Sandbox Code Playgroud)
我还修复了TextInputLayout.setError()在清除同一类中的错误后留下空白空间.
这实际上是 Google 的一个已知问题。
https://issuetracker.google.com/issues/37051832
他们提出的解决方案是创建一个自定义 TextInputEditText 类
class MyTextInputEditText : TextInputEditText {
@JvmOverloads
constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = android.R.attr.editTextStyle
) : super(context, attrs, defStyleAttr) {
}
private val parentRect = Rect()
override fun getFocusedRect(rect: Rect?) {
super.getFocusedRect(rect)
rect?.let {
getMyParent().getFocusedRect(parentRect)
rect.bottom = parentRect.bottom
}
}
override fun getGlobalVisibleRect(rect: Rect?, globalOffset: Point?): Boolean {
val result = super.getGlobalVisibleRect(rect, globalOffset)
rect?.let {
getMyParent().getGlobalVisibleRect(parentRect, globalOffset)
rect.bottom = parentRect.bottom
}
return result
}
override fun requestRectangleOnScreen(rect: Rect?): Boolean {
val result = super.requestRectangleOnScreen(rect)
val parent = getMyParent()
// 10 is a random magic number to define a rectangle height.
parentRect.set(0, parent.height - 10, parent.right, parent.height)
parent.requestRectangleOnScreen(parentRect, true /*immediate*/)
return result;
}
private fun getMyParent(): View {
var myParent: ViewParent? = parent;
while (!(myParent is TextInputLayout) && myParent != null) {
myParent = myParent.parent
}
return if (myParent == null) this else myParent as View
}
}```
Run Code Online (Sandbox Code Playgroud)