【发布时间】:2019-12-16 23:36:14
【问题描述】:
我使用这个库中的TextInputLayout:
implementation 'com.google.android.material:material:1.0.0'
我在布局中有以下部分:
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/phoneNumberLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:errorEnabled="true">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/phoneNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/label_phone_number"/>
</com.google.android.material.textfield.TextInputLayout>
我启用了 Kotlin Android 扩展,我尝试将 TextWatcher 分配给 phoneNumber,如下所示:
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
phoneNumber.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable?) {
Log.e("DialogFragment", "phoneNumber::onTextChanged()")
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
Log.e("DialogFragment", "phoneNumber current text: " + s)
}
})
}
当我开始打字时什么都没有发生,浪费了几个小时...任何帮助将不胜感激。
【问题讨论】:
-
提供的 sn-p 看起来不错。检查您是否附加到正确的视图。布局中可能有多个具有相同 id 的视图?
-
onActivityCreated似乎是错误的地方。试试onViewCreated,你的布局被膨胀并分配给一个片段。
标签: android kotlin android-edittext android-dialogfragment textwatcher