![](/img/trans.png)
[英]android: How to replace a word with another text in edittext using addTextChangedListener?
[英]How to get full text from the editText by using addTextChangedListener in kotlin
我想将 editText 中的全文添加到数组中。 但是在运行此代码时,数组值将像第一个单词、第二个单词等。如何获得完整的句子,我想将它添加到一个数组中(已经使用接口完成了)
注:代码放在recyclerview适配器上,其中texBox为EditText,examineListener为使用的接口
在这里我分享了代码。 请检查一下。
textBox.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable) {
textFromBox = s.toString()
examinationListener.addAnswer(textFromBox)
}
override fun beforeTextChanged(s: CharSequence, start: Int,
count: Int, after: Int) {
}
override fun onTextChanged(s: CharSequence, start: Int,
before: Int, count: Int) {
}
})
override fun afterTextChanged(s: Editable) {
textFromBox = textBox.text.toString()
examinationListener.addAnswer(textFromBox)
}
我创建了一个列表大小的数组。 RecyclerView 的每一项都有一个 EditText。
val array = arrayOfNulls<String>(listSize)
我们需要知道 EditText 的 position 才能添加到数组中。 改变界面如下:
interface ExaminationListener {
fun addAnswer(text: String, position: Int)
}
function addAnswer:
override fun addAnswer(text: String, position: Int) {
array[position] = text // we add every changes to the array.
}
这种用法更有用。 我们不需要其他方法:
textBox.doAfterTextChanged {
examinationListener.addAnswer(it.toString(), position) // you can use adapterPosition if that is in Viewholder
}
我们从数组元素创建一个句子。 你可以看到你的完整句子:
val sentence = array.filterNotNull().joinToString (separator = " ") { it -> it }
Log.d("Sentence" , sentence)
非常简单,只需将TextWatcher
添加到您的EditText
中,例如:
val answerWatcher = object : TextWatcher {
override fun beforeTextChanged(value: CharSequence, start: Int, count: Int, after: Int) {
}
override fun onTextChanged(value: CharSequence, start: Int, before: Int, count: Int) {
when {
// Here value is your full text from your EditText
value.toString().equals("I am a sentence", ignoreCase = true) -> {
}
else -> {
// Just a test condition
}
}
}
override fun afterTextChanged(s: Editable) {
}
}
// Add this TextWatcher to your EditText
edittext.addTextChangedListener(answerWatcher)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.