Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/198.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在edittext中更改文本之前获取光标起始索引_Android_Android Edittext_Selection_Textwatcher - Fatal编程技术网

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

Android 在edittext中更改文本之前获取光标起始索引,android,android-edittext,selection,textwatcher,Android,Android Edittext,Selection,Textwatcher,这似乎是一个简单的问题,但我不明白为什么会发生这种情况。我想在用户更改文本之前在edittext中获取选择的开始和结束索引,因此在我的TextWatcher中,我做了如下操作:- public void beforeTextChanged(CharSequence s, int start, int count, int after) { selectionStart = edittext.getSelectionStart();

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

 public void beforeTextChanged(CharSequence s, int start, int count, int after) {


                    selectionStart = edittext.getSelectionStart();
                    selectionEnd = edittext.getSelectionEnd();


            }

但是,selectionStart和selectionEnd都返回所选内容的结束索引。例如,我选择了一个单词“hello”两者都返回5。我尝试在任何键盘输入之前将它们插入处理程序以获取选择,但没有任何效果。

我最终找到了解决此问题的方法。这有点难看,但最有效地解决了此问题。问题是,在文本更改之前,文本选择已经消失,因此我必须使用处理程序等待文本更改选择和范围观察程序,以便在选择后立即进行设置,如下所示:-

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
}

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

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
}

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

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