2016-04-11 1 views
0

J'ai une chaîne android:Comment associer un mot cliquable avec TextWatcher dans EditText dans android?

String meow = "this_is_part1_and_here_another_and_another_one"; 

J'ai fait chaque cliquable mot à l'aide Clickable durée.

Voici le code pour mettre en oeuvre la durée cliquable pour chaque mot (adapté à partir d'une pile de réponses trop-plein)

String meow = "this_is_part1_and_here_another_and_another_one"; 
    String[] tokens = meow.split("_"); 

    ArrayList<String> tokenList = new ArrayList<String>(Arrays.asList(tokens)); 


    if (meow != null || meow.length() != 0 ) { 
     _field.setMovementMethod(LinkMovementMethod.getInstance()); 
     _field.setText(addClickablePart(meow, tokenList), EditText.BufferType.SPANNABLE); 


    // _field.addTextChangedListener(new TextWatcher() { 
    // 
    // @Override 
    // public void afterTextChanged(Editable s) { 
    // Editable ab = new SpannableStringBuilder(s.toString().replace("A", "a")); 
    // _field.setText(ab); 
    // Toast.makeText(getApplicationContext(), s.toString(),Toast.LENGTH_LONG).show(); 
    // 
    //  } 
    // 
    // @Override 
    // public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
    //  } 
    // 
    // @Override 
    // public void onTextChanged(CharSequence s, int start, int before, int count) { 
    // 
    //  } 
    //  }); 

} 



private SpannableStringBuilder addClickablePart(String str, ArrayList<String> clickableWords) { 
    SpannableStringBuilder ssb = new SpannableStringBuilder(str); 

    for (String clickableWord : clickableWords) { 
     int idx1 = str.indexOf(clickableWord); 
     int idx2 = 0; 
     while (idx1 != -1) { 
      idx2 = idx1 + clickableWord.length(); 
      final String clickString = str.substring(idx1, idx2); 
      ssb.setSpan(new MyClickableSpan(clickString), idx1, idx2, 0); 
      idx1 = str.indexOf(clickableWord, idx2); 
     } 
    } 

    return ssb; 
} 



class MyClickableSpan extends ClickableSpan { 



String clicked; 
public MyClickableSpan(String string) { 
    super(); 
    clicked = string; 
    } 

public void onClick(View widget) { 
    Toast.makeText(widget.getContext(), clicked, Toast.LENGTH_LONG).show(); 

    } 

public void updateDrawState(TextPaint ds) {// override updateDrawState 
    ds.setUnderlineText(false); // set to false to remove underline 
    } 

} 

Lorsque je tape un mot particulier, je montre un toast de ce mot. Maintenant, ce que je veux, c'est quand je tape sur ce mot particulier, après avoir montré ce mot pendant quelques secondes, ce mot deviendra vide dans EditText, et je tape un nouveau mot à sa place, et obtenir cette chaîne particulière.

Ce qui suit est par exemple montré dans EditText:

this_is_part1_and_here_another_and_another_one

je tape sur le mot part1. Après avoir montré part1 en pain grillé pendant quelques secondes, le mot devient vide dans EditText comme ceci:

this_is_ _and_here_another_and_another_one

Maintenant, je tape un mot nouveau à sa place dire woof

this_is_woof_and_here_another_and_another_one

An d Je peux stocker le mot woof dans une chaîne.

Comment faire? En bref, comment associer mes mots cliquables à TextWatcher? J'ai commenté la partie TextWatcher car je ne sais pas comment l'associer à ClickableSpan. S'il vous plait aidez moi avec ceci. Merci.

+0

Quelle chaîne stocker le mot 'woof'? –

+0

C'est juste un exemple. Je veux stocker tous les mots cliquables précédents et tous les mots édités dans la place de ce mot dans une carte/multimap –

Répondre

0

Je ne pense pas que vous pouvez "hypertexte" mots différents dans une durée. Vous obtenez juste la vue de texte sur l'écouteur de clic. Je vous suggère de construire un ViewGroup qui encapsule un ou plusieurs mots et peut répondre aux clics individuels de mots,

class HyperText extends LinearLayout { 
    ... 
    public static interface OnTextClickedListener { 
    public void onTextClicked(String text); 
    } 


    public void addText(String text) { 
    TextView tv = new TextView(...); // or inflate it from somewhere 
    tx.setOnClickListener(...); // call onTextClicked(this.getText()); 
    addView(tv); 
    } 
} 

Quelque chose comme ça.

+0

Non, je crée des travées pour chaque mot individuel. J'ai juste besoin d'un mécanisme pour taper sur un mot, puis remplacer ce mot en tapant et récupérer le mot tapé. –

+0

Oui, voir ci-dessus. Il n'y a rien dans Android qui va faire ça pour vous. Vous devez écrire le code vous-même. –