我有几个EditText
并且我希望从TextInputLayout
中消除错误,一旦用户更改了文本,我该怎么做才能检测到所有Textview
中的文本发生变化1}} S'我想,正在实施一些课程,但我不知道哪个,这是假设这是解决方案,我是kotlin的新手,对不起,如果这个问题看起来很愚蠢
class AuxiliaryChangesInfoActivity: AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_auxiliary_changes_info)
hideErrorMessage()
img_close_auxiliary_changes.setOnClickListener({
onBackPressed()
})
txt_save_auxiliary_changes.setOnClickListener({
if(txt_description_auxiliary_charges_info.text.isEmpty()){
tI_description_auxiliary_charges_info.error="You need to specify a Description"
}
else if(txt_frequency_auxiliary_charges_info.text.isEmpty()){
tI_frequency_auxiliary_charges_info.error="You need to specify a frequency"
}
else if(txt_to_auxiliary_charges_info.text.isEmpty()){
tI_to_auxiliary_charges_info.error="You need to specify a to value"
}
else if(txt_from_auxiliary_charges_info.text.isEmpty()){
tI_from_auxiliary_charges_info.error="You need to specify a from value"
}
else if(txt_qty_auxiliary_charges_info.text.isEmpty()){
tI_qty_auxiliary_charges_info.error="You need to specify a quantity value"
}
})
}
@OnTextChanged(R.id.txt_description_auxiliary_charges_info,
R.id.txt_frequency_auxiliary_charges_info,
R.id.txt_from_auxiliary_charges_info,
R.id.txt_to_auxiliary_charges_info,
R.id.txt_qty_auxiliary_charges_info,
callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
fun hideErrorMessage() {
// your code here
tI_description_auxiliary_charges_info.isErrorEnabled = false
tI_frequency_auxiliary_charges_info.isErrorEnabled = false
tI_from_auxiliary_charges_info.isErrorEnabled = false
tI_to_auxiliary_charges_info.isErrorEnabled=false
tI_qty_auxiliary_charges_info.isErrorEnabled=false
}
答案 0 :(得分:0)
添加CustomEditText
类
class CustomEditText : EditText {
private var mContext: Context? = null
private var attributeSet: AttributeSet? = null
constructor(context: Context) : super(context) {
this.mContext = context
initCustomEditText()
}
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
this.mContext = context
this.attributeSet = attrs
initCustomEditText()
}
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
this.mContext = context
this.attributeSet = attrs
initCustomEditText()
}
private fun initCustomEditText() {
this.addTextChangedListener(object : TextWatcher {
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
}
override fun afterTextChanged(arg0: Editable) {
if(context is MainActivity){
(context as MainActivity).tI_description_auxiliary_charges_info.isErrorEnabled=false
}else if(context is MainActivity2){
(context as MainActivity2).tI_description_auxiliary_charges_info.isErrorEnabled=false
}
// ... other Activity
}
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
})
}
}
确保 -
您的MainActivity
(由于您未提及而不知道您的身份)和tI_description_auxiliary_charges_info
两者都是public
XML中的所有EditText
都更改为com.a4plus1.com.dhq_app.Components.CustomEditText
(包名.CustomEditText)
答案 1 :(得分:0)
如果我理解,您希望在多个TextInputLayouts之间共享一个TextWatcher。可能有几种可能的解决方案;一种方法是:
遍历视图,将textWatcher添加到每个TextInputLayout
// app module's build.gradle
dependencies {
// #1
implementation 'androidx.core:core-ktx:0.3'
}
// sample Activity.kt
private lateinit var layout: ConstraintLayout
override fun onCreate(savedInstanceState: Bundle?) {
layout = findViewById(R.id.coordinator)
addTextWatcherToEditTexts()
}
// #2
private val textWatcher = object : TextWatcher {
override fun afterTextChanged(s: Editable?) {
layout.children.forEach { view ->
(view as? TextInputLayout)?.let {
with(it) {
error = null
isErrorEnabled = false
}
}
}
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
// no-op
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
// no-op
}
}
// #3
private fun addTextWatcherToEditTexts() {
layout.children.forEach { view ->
(view as? EditText)?.addTextChangedListener(textWatcher)
}
}
答案 2 :(得分:0)
如果您不介意添加新的依赖项,则应考虑使用Butterknife。 它与kotlin兼容,你可以用它来写这样的东西:
@OnTextChanged(R.id.txt_description_auxiliary_charges_info,
R.id.txt_frequency_auxiliary_charges_info,
R.id.third_edit_text,
...,
callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
fun hideErrorMessage(editable: Editable) {
// your code here
// tI_description_auxiliary_charges_info.isErrorEnabled = false
}
要将其添加到项目中,请将其添加到项目build.gradle
dependencies {
...
classpath "com.jakewharton:butterknife-gradle-plugin:$butterknife_version"
}
这是你的app build.gradle
apply plugin: 'kotlin-kapt'
dependencies {
...
compile "com.jakewharton:butterknife:$butterknife_version"
kapt "com.jakewharton:butterknife-compiler:$butterknife_version"
}