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.
Quelle chaîne stocker le mot 'woof'? –
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 –