我需要为用 TextInputLayout
包装的 TextInputEditText
做一些输入验证。
如果输入格式错误,我希望错误出现在该行下方。
所有逻辑都在 View 模型而不是 View ( fragment 或 Activity )中完成。但我似乎无法通过 viewmodel 访问 View ,例如:
textinputlayout.setError("error")
在 View 模型中不起作用
并且 layout.findViewbyId(layoutId)
在 View 模型中也不起作用。
有什么想法吗?
最佳答案
使用下面的代码在 TextInputLayout 中设置错误..
class SpinerActivity :AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.spiner)
setEdittext()
}
private fun setEdittext() {
var textError:TextInputLayout=findViewById(R.id.amEt1)
textError.error="Please Enter Name"
}
关于java - 如何使用 Kotlin 在 View 模型中使用 TextInputLayout 设置错误标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49508402/