【问题标题】:TextWatcher warnings and slow type in androidandroid中的TextWatcher警告和慢速输入
【发布时间】:2017-03-29 08:29:06
【问题描述】:

我想在 android 中更改输入字符。如果用户输入 space 或 - 它将更改为 _

所以我尝试了:

TextWatcher tt = null;

final EditText etUsername = (EditText) findViewById(R.id.etUsername);
   tt = new TextWatcher() {
        public void afterTextChanged(Editable s){
            etUsername.setSelection(s.length());
        }
        public void beforeTextChanged(CharSequence s,int start,int count, int after){}
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            etUsername.removeTextChangedListener(tt);
            etUsername.setText(etUsername.getText().toString().replace(" ", "_"));
            etUsername.setText(etUsername.getText().toString().replace("-", "_"));
            etUsername.addTextChangedListener(tt);
        }
    };
    etUsername.addTextChangedListener(tt);

它“有效”,但如果用户输入速度很快,一些字母将不会出现,我会收到一些警告:

W/IInputConnectionWrapper: getSelectedText on inactive InputConnection
W/IInputConnectionWrapper: requestCursorAnchorInfo on inactive InputConnection
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper: commitText on inactive InputConnection
W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
W/IInputConnectionWrapper: getSelectedText on inactive InputConnection
W/IInputConnectionWrapper: requestCursorAnchorInfo on inactive InputConnection
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper: commitText on inactive InputConnection
W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
W/IInputConnectionWrapper: getSelectedText on inactive InputConnection
W/IInputConnectionWrapper: requestCursorAnchorInfo on inactive InputConnection
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper: commitText on inactive InputConnection
W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
W/IInputConnectionWrapper: getSelectedText on inactive InputConnection
W/IInputConnectionWrapper: requestCursorAnchorInfo on inactive InputConnection
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper: commitText on inactive InputConnection
W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
W/IInputConnectionWrapper: getSelectedText on inactive InputConnection
W/IInputConnectionWrapper: requestCursorAnchorInfo on inactive InputConnection
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
W/IInputConnectionWrapper: finishComposingText on inactive InputConnection

有什么想法吗?

【问题讨论】:

    标签: android android-edittext android-textwatcher


    【解决方案1】:

    无需在文本更改时一次又一次地删除和添加文本更改侦听器,只需设置一个条件来检查您的可编辑项是否具有“-”或“”,然后只需替换并将其设置为 EditText。

    etUsername.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
                }
    
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    if (s.length() > 0 && (s.toString().contains("-") || s.toString().contains(" "))) {
                        etUsername.setText(s.toString().replace("-", "_").replace(" ", "_"));
                    }
                }
    
                @Override
                public void afterTextChanged(Editable s) {
                    etUsername.setSelection(s.length());
                }
            });
    

    【讨论】:

      【解决方案2】:

      试试这个 Textwatcher

        TextWatcher watch = new TextWatcher(){
      
          @Override
          public void afterTextChanged(Editable arg0) {
              // TODO Auto-generated method stub
      
          }
      
          @Override
          public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                  int arg3) {
              // TODO Auto-generated method stub
      
          }
      
          @Override
          public void onTextChanged(CharSequence s, int a, int b, int c) {
              // TODO Auto-generated method stub
      
              output.setText(s);
              if(a == 9){
                  Toast.makeText(getApplicationContext(), "Maximum Limit Reached", Toast.LENGTH_SHORT).show();
              }
          }};
      

      【讨论】:

      • 更多指南可在 android 开发者中获得 - link
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-03
      • 1970-01-01
      • 2019-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-16
      相关资源
      最近更新 更多