<i id='ymyR1'><tr id='ymyR1'><dt id='ymyR1'><q id='ymyR1'><span id='ymyR1'><b id='ymyR1'><form id='ymyR1'><ins id='ymyR1'></ins><ul id='ymyR1'></ul><sub id='ymyR1'></sub></form><legend id='ymyR1'></legend><bdo id='ymyR1'><pre id='ymyR1'><center id='ymyR1'></center></pre></bdo></b><th id='ymyR1'></th></span></q></dt></tr></i><div id='ymyR1'><tfoot id='ymyR1'></tfoot><dl id='ymyR1'><fieldset id='ymyR1'></fieldset></dl></div>

      <tfoot id='ymyR1'></tfoot>

    1. <legend id='ymyR1'><style id='ymyR1'><dir id='ymyR1'><q id='ymyR1'></q></dir></style></legend>

      • <bdo id='ymyR1'></bdo><ul id='ymyR1'></ul>

      <small id='ymyR1'></small><noframes id='ymyR1'>

      如何使用 TextWatcher 更新相同的 EditText?

      时间:2023-05-28
      • <bdo id='m4ZCq'></bdo><ul id='m4ZCq'></ul>
        • <small id='m4ZCq'></small><noframes id='m4ZCq'>

        • <tfoot id='m4ZCq'></tfoot>
          <i id='m4ZCq'><tr id='m4ZCq'><dt id='m4ZCq'><q id='m4ZCq'><span id='m4ZCq'><b id='m4ZCq'><form id='m4ZCq'><ins id='m4ZCq'></ins><ul id='m4ZCq'></ul><sub id='m4ZCq'></sub></form><legend id='m4ZCq'></legend><bdo id='m4ZCq'><pre id='m4ZCq'><center id='m4ZCq'></center></pre></bdo></b><th id='m4ZCq'></th></span></q></dt></tr></i><div id='m4ZCq'><tfoot id='m4ZCq'></tfoot><dl id='m4ZCq'><fieldset id='m4ZCq'></fieldset></dl></div>

              <tbody id='m4ZCq'></tbody>
                <legend id='m4ZCq'><style id='m4ZCq'><dir id='m4ZCq'><q id='m4ZCq'></q></dir></style></legend>
                本文介绍了如何使用 TextWatcher 更新相同的 EditText?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                在我的 Android 应用程序中,我需要实现一个 TextWatcher 接口来实现 onTextChanged.我遇到的问题是,我想用一些额外的字符串更新相同的 EditText.当我尝试这样做时,程序会终止.

                In my Android application I need to implement a TextWatcher interface to implement onTextChanged. The problem I have is, I want to update the same EditText With some extra string. When I try to do this the program terminates.

                 final EditText ET = (EditText) findViewById(R.id.editText1);
                 ET.addTextChangedListener(new TextWatcher() {
                
                        @Override
                        public void onTextChanged(CharSequence s, int start, int before, int count)
                        {
                            try
                            {
                                 ET.setText("***"+ s.toString());
                                 ET.setSelection(s.length());
                            }
                            catch(Exception e)
                            {
                                Log.v("State", e.getMessage());
                            }
                        }
                
                        @Override
                        public void beforeTextChanged(CharSequence s, int start, int count, int after)
                        {
                
                        }
                
                        @Override
                        public void afterTextChanged(Editable s)
                        {               
                        }
                    });
                

                我的程序终止了,即使我尝试在我的代码中捕获异常,它仍然终止.有谁知道为什么会发生这种情况以及我如何做到这一点?谢谢.

                My program terminates and even I try to catch the exception like in my code still it terminates. Does anyone have any idea why this happens and how I can achieve this? Thanks.

                推荐答案

                TextView 的内容在 onTextChanged 事件上不可编辑.

                The content of the TextView is uneditable on the onTextChanged event.

                相反,您需要处理 afterTextChanged 事件才能对文本进行更改.

                Instead, you need to handle the afterTextChanged event to be able to make changes to the text.

                更详尽的解释参见:Android TextWatcher.afterTextChanged vs TextWatcher.onTextChanged

                注意:错误onTextChanged

                显然,您正在通过不断更改 afterTextChanged 事件上的 text 导致无限循环.

                Obvioulsy, you are causing an endless loop by continuously changing the text on afterTextChanged event.

                来自 参考:

                public abstract void afterTextChanged (Editable s)
                调用此方法是为了通知您,在 s 中的某处,文本已被改变了.从此对 s 进行进一步更改是合法的回调,但注意不要让自己陷入无限循环,因为您所做的任何更改都会导致再次调用此方法递归地....

                • 建议1:如果可以的话,检查s是否已经在事件触发时是你想要的.

                  • Suggestion 1: if you can, check if the s is already what you want when the event is triggered.

                    @Override
                    public void afterTextChanged(Editable s)
                    {    
                        if( !s.equalsIngoreCase("smth defined previously"))
                             s = "smth defined previously";              
                    }
                    

                  • 建议 2:如果您需要做更复杂的事情(格式化、验证)您可以使用 synchronized 方法-textwatcher">这个发帖.
                  • Suggestion 2: if you need to do more complex stuff (formatting, validation) you can maybe use a synchronized method like in this post.
                  • 注意 2:将输入格式化为部分隐藏,并用 n 个星号直到最后一个 4 个字符(****四个)

                    Note 2 : Formatting the input as partially hidden with n stars till the last 4 chars ( ****four)

                    您可以在建议 1 中使用类似的内容:

                    You can use something like this in suggestion 1:

                        @Override
                        public void afterTextChanged(Editable s)
                        {    
                           String sText = ET.getText().toString()
                    
                            if( !isFormatted(sText))
                                 s = format(sText);              
                        }
                        bool isFormatted(String s)
                        {
                         //check if s is already formatted
                        }
                    
                        string format(String s)
                        {
                          //format s & return
                        }
                    

                    这篇关于如何使用 TextWatcher 更新相同的 EditText?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:在 Android 项目中使用 EditText.toString() 方法获取奇怪的文本.为什么? 下一篇:根据 EditText 值更改 SeekBar 进度

                相关文章

                1. <tfoot id='w4qz9'></tfoot>
                  <legend id='w4qz9'><style id='w4qz9'><dir id='w4qz9'><q id='w4qz9'></q></dir></style></legend>

                  <small id='w4qz9'></small><noframes id='w4qz9'>

                    <bdo id='w4qz9'></bdo><ul id='w4qz9'></ul>
                  <i id='w4qz9'><tr id='w4qz9'><dt id='w4qz9'><q id='w4qz9'><span id='w4qz9'><b id='w4qz9'><form id='w4qz9'><ins id='w4qz9'></ins><ul id='w4qz9'></ul><sub id='w4qz9'></sub></form><legend id='w4qz9'></legend><bdo id='w4qz9'><pre id='w4qz9'><center id='w4qz9'></center></pre></bdo></b><th id='w4qz9'></th></span></q></dt></tr></i><div id='w4qz9'><tfoot id='w4qz9'></tfoot><dl id='w4qz9'><fieldset id='w4qz9'></fieldset></dl></div>