gpt4 book ai didi

android - 键入时不调用TextInputEditText addTextChangedListener

转载 作者:行者123 更新时间:2023-12-02 13:22:24 25 4
gpt4 key购买 nike

我有一个带有TextInputEditText( Material 设计组件)的简单表单,无法收听文本更改。用TextWatcher添加addTextChangedListener似乎效果为零。当我运行我的应用程序并使用虚拟键盘输入输入时,没有调用任何回调(未击中调试断点),并且TextView没有更新:

Activity :

class MainActivity : AppCompatActivity() {


private lateinit var webBankingIdLayout: TextInputLayout
private lateinit var webBankingIdInput: TextInputEditText
private lateinit var textView: TextView

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
webBankingIdLayout = findViewById<TextInputLayout>(R.id.web_banking_id_input_layout)
webBankingIdInput = TextInputEditText(webBankingIdLayout.context)
textView = findViewById<TextView>(R.id.temp_text)

webBankingIdInput.addTextChangedListener(object: TextWatcher {
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
s!!
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
s!!
textView.text = s!!
}
override fun afterTextChanged(s: Editable?) {
s!!
}
})
}
}

Activity 布局:

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/web_banking_id_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/web_banking_id_text_input"
android:maxLines="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:inputType="text"
android:hint="@string/web_banking_id_hint" />

</com.google.android.material.textfield.TextInputLayout>

<TextView
android:id="@+id/temp_text"
android:layout_width="250dp"
android:layout_height="113dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/password_input_layout" />

但是,如果我以编程方式设置输入字段的文本,则将调用回调并更新 TextView
webBankingIdInput.text = "Yippee!"

为什么我的代码不起作用,如何收听字符输入到文本输入中?

我的目标是隐藏按钮,直到在该字段中输入的字符数最少为止。

最佳答案

您需要更换:

webBankingIdInput = TextInputEditText(webBankingIdLayout.context)

至:
webBankingIdInput = findViewById<TextInputEditText>(R.id.web_banking_id_text_input)

关于android - 键入时不调用TextInputEditText addTextChangedListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53124844/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com