代码之家  ›  专栏  ›  技术社区  ›  Omar Boshra

在edittext中更改文本之前获取光标起始索引

  •  1
  • Omar Boshra  · 技术社区  · 7 年前

    这可能看起来像一个简单的问题,但我不明白为什么会发生这种情况。我想在用户更改文本之前,在我的edittext中获取所选内容的开始和结束索引,因此在我的TextWatcher中,我做了如下操作:-

     public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
    
                        selectionStart = edittext.getSelectionStart();
                        selectionEnd = edittext.getSelectionEnd();
    
    
                }
    

    但是,selectionStart和selectionEnd都返回所选内容的结束索引。例如,我选择了一个单词“ 你好

    2 回复  |  直到 7 年前
        1
  •  0
  •   Omar Boshra    7 年前

    我终于找到了这个问题的解决方案,虽然有点难看,但最有效地解决了这个问题。问题是,在文本更改之前,文本选择已经消失,因此我必须使用处理程序等待文本被选择,然后使用范围观察程序立即设置选择,就像这样:-

    final SpanWatcher watcher = new SpanWatcher() {
    @Override
    public void onSpanAdded(final Spannable text, final Object what,final int start, final int end) {
    
    final Handler handler = new Handler();//handler to get selection after 
    certain time
    handler.postDelayed(new Runnable() {
    @Override
    public void run() {
    
    if(noteview.hasSelection()) {
    
    higlightdetect=1;//integer to detect that text was selected
    
    selectionStart = noteview.getSelectionStart();
    selectionEnd = noteview.getSelectionEnd();
    
                        }else
    higlightdetect=0;
                    }
                }, 600);//after 600ms seemed to be the optimal time after selection occurs
            }
    }
    
    @Override
    public void onSpanRemoved(final Spannable text, final Object what,
                                      final int start, final int end) {
                // Nothing here.
            }
    
    @Override
    public void onSpanChanged(final Spannable text, final Object 
    what,final int ostart, final int oend, final int nstart, final int nend) 
    {
    //The same
    
    final Handler handler = new Handler();//handler to get selection after 
    certain time
    handler.postDelayed(new Runnable() {
    @Override
    public void run() {
    
    if(noteview.hasSelection()) {
    
    higlightdetect=1;//integer to detect that text was selected
    
    selectionStart = noteview.getSelectionStart();
    selectionEnd = noteview.getSelectionEnd();
    
                        }else
    higlightdetect=0;
                    }
                }, 600);//after 600ms seemed to be the optimal time after selection occurs
            }
    };
    
    
    edittext.getText().setSpan(watcher, 0, edittext.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);//set spanwatcher to edittext
    

    @Override
    public void beforeTextChanged(CharSequence s, int start, int 
    count, int after) {
    
    
    
    if(higlightdetect==0){//condition to get cursor position without selection
    
    selectionStart = noteview.getSelectionStart();
    selectionEnd=selectionStart;
    }
    public void onTextChanged(CharSequence s, int start, int before, int 
    count) {
    
            }
    
    @Override
    public void afterTextChanged(Editable s) {
    
    Toast.makeText(getthenote.this, String.valueOf(selectionStart), 
    Toast.LENGTH_SHORT).show();
    Toast.makeText(getthenote.this, String.valueOf(selectionEnd), 
    Toast.LENGTH_SHORT).show();
    
    higlightdetect=0 //resetting higlightdetect
    }
    
        2
  •  0
  •   htafoya    5 年前

    你为什么不直接使用 start 中的参数 beforeTextChanged 方法