<tfoot id='VAXFu'></tfoot>
    <bdo id='VAXFu'></bdo><ul id='VAXFu'></ul>

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

  • <legend id='VAXFu'><style id='VAXFu'><dir id='VAXFu'><q id='VAXFu'></q></dir></style></legend>

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

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

        时间:2023-10-01

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

                  <tbody id='RoiWu'></tbody>

                <legend id='RoiWu'><style id='RoiWu'><dir id='RoiWu'><q id='RoiWu'></q></dir></style></legend>

                1. 本文介绍了如何使用 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在 Android 项目中使用 EditText.toString() 方法获取奇 下一篇:根据 EditText 值更改 SeekBar 进度

                  相关文章

                  最新文章

                  <small id='521e2'></small><noframes id='521e2'>

                  <tfoot id='521e2'></tfoot>

                    • <bdo id='521e2'></bdo><ul id='521e2'></ul>

                  1. <i id='521e2'><tr id='521e2'><dt id='521e2'><q id='521e2'><span id='521e2'><b id='521e2'><form id='521e2'><ins id='521e2'></ins><ul id='521e2'></ul><sub id='521e2'></sub></form><legend id='521e2'></legend><bdo id='521e2'><pre id='521e2'><center id='521e2'></center></pre></bdo></b><th id='521e2'></th></span></q></dt></tr></i><div id='521e2'><tfoot id='521e2'></tfoot><dl id='521e2'><fieldset id='521e2'></fieldset></dl></div>
                    <legend id='521e2'><style id='521e2'><dir id='521e2'><q id='521e2'></q></dir></style></legend>