J'ai finalement trouvé une solution à ce problème un peu moche, mais résout le problème de façon optimale. Le problème était qu'avant textchanged la sélection de texte était déjà parti, j'ai dû utiliser un gestionnaire pour attendre quand le texte est sélectionné Watcher pour définir les sélections immédiatement après comme ceci: -
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
Bien sûr, cela ne fonctionne que lorsque le texte est sélectionné ne peux pas l'utiliser pour la saisie régulière car il mise à jour automatique instantanément après le texte est modifié de façon à le veilleur de texte j'ai ajouté ce code: -
@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
}