【问题标题】:Android two TextInputEditText and two way addTextChangedListenerAndroid 两种TextInputEditText 和两种方式addTextChangedListener
【发布时间】:2017-02-06 14:16:21
【问题描述】:

我有两个 TextInputEditText 字段:

TextInputEditText fromData = (TextInputEditText) view.findViewById(R.id.fromData);
TextInputEditText toData = (TextInputEditText) view.findViewById(R.id.toData );

两个字段都有addTextChangedListener() 并且应该更改彼此的文本。但这会导致错误(递归)。因为当我在fromData中写入一些文本时,toData文本也会改变,如果toData文本改变fromData也应该改变。

如何让 textchangelistener 以两种方式工作?

【问题讨论】:

    标签: android android-edittext addtextchangedlistener


    【解决方案1】:

    覆盖侦听器,如果要更改的文本与已更改的文本相同,请不要更改它。

    因此,fromData 中的文本发生了更改,并且它调用了 on text changed 侦听器在 on text changed 检查正在更改的文本是否等于 textview 中已经存在的文本,如果它不做任何事情。

    onTextChanged(String newText) {
         if (!newText.equals(fromData.getText())) {
               fromData.setText(newText);
         }
    }
    

    【讨论】:

      【解决方案2】:

      你可以定义一个像 focusedTextView 这样的全局变量,然后添加触摸监听器到两个文本视图。现在,当触摸特定视图时。

      您只需将此变量的值设置为该视图。

      最后,在您的 addTextChangeListener 只需检查 此全局变量的当前值是什么,然后更改其他 textView 的文本不专注。

      希望这将为您提供所需的两种结果。

      如果它对你有任何改变,请告诉我。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-08
        • 1970-01-01
        • 2015-08-09
        • 2017-09-05
        • 2014-02-13
        相关资源
        最近更新 更多