2017-07-17 1 views
1

Cela peut sembler une question simple, mais je ne comprends pas pourquoi cela se produit. Je veux obtenir les index de début et de fin d'une sélection dans mon edittext avant que l'utilisateur change le texte dans mon TextWatcher je ne comme suit: -Obtenir le curseur de départ index avant le texte a changé dans edittext

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


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


      } 

Toutefois, les deux selectionStart et selectionEnd retourne l'index de fin de l'exemple selection.For sélectionner un mot « bonjour » à la fois retour .Je a essayé de les insérer dans un gestionnaire pour obtenir la sélection avant toute entrée au clavier en vain.

Répondre

0

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 
}