代码之家  ›  专栏  ›  技术社区  ›  Pop-A-Stash

键入时未调用TextInputItemText addTextChangedListener

  •  0
  • Pop-A-Stash  · 技术社区  · 7 年前

    我有一个简单的表格 TextInputEditText (材质设计组件)并且我无法侦听文本更改。添加 TextWatcher addTextChangedListener 似乎没有任何影响。当我运行我的应用程序并用虚拟键盘输入时,没有调用任何回调(没有命中调试断点),我的 TextView 未更新:

    活动:

    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!!
                }
            })
        }
    }
    

    活动布局:

    <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" />
    

    但是,如果我以编程方式设置输入字段的文本,则调用回调并 已更新。

    webBankingIdInput.text = "Yippee!"
    

    为什么我的代码不工作,我怎么能听字符输入到文本输入?

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

    1 回复  |  直到 7 年前
        1
  •  3
  •   Jose Alberto Navarro Barrios    7 年前

    您需要替换:

    webBankingIdInput = TextInputEditText(webBankingIdLayout.context)
    

    收件人:

    webBankingIdInput = findViewById<TextInputEditText>(R.id.web_banking_id_text_input)
    
        2
  •  0
  •   James Nick Sears    6 年前

    我有一个类似的问题,这是由于没有在XML中设置android:inputType参数引起的。