evento de clique do span do Android
Está bem. Esses são meus problemas.
Eu preciso usar expressões regulares para filtrar tudo exceto letras e então eu preciso encapsular as palavras encontradas dentro de uma tag $ word. Com este str = str.replaceAll (padrão, "$ 0");. agora estou filtrando todos os elementos corretos (pontuação, números etc) mas encapsulando cada letra dentro de cada palavra em uma tag e não na palavra. Então, como eu uso a expressão regular para agrupar as letras em uma palavra?de "(uma tag aberta) t (um fechamento) (uma tag aberta) h (um fechamento) (uma tag aberta) i (um fechamento) (uma tag aberta) s (um fechamento) (uma tag aberta) i (a tag close) (uma tag aberta) s (a fechar) (uma tag aberta) w (a fechar) (uma tag aberta) r (a fechar) (uma tag aberta) o (a fechar) (a tag open) n (a fechar) (uma etiqueta aberta) g (a fechar) ";
para :
"(uma tag aberta) Este (um close) (uma tag aberta) é (um close) (uma tag aberta) direita (um close)";
então eu estou fazendo-os clicáveis e eu preciso pegar o evento click e obter a posição na tela da palavra clicada como eu quero usar o evento clicado para fazer uma dica de ferramenta aparecer logo abaixo da palavra clicada. obrigado pela ajuda.
classe pública MainActivity estende Activity {
public String text = "This is just a sentence to test you. 23 this is another number23!g?";
public TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = explode(text);
tv = (TextView) findViewById(R.id.tv1);
tv.setLinksClickable(true);
tv.setMovementMethod(LinkMovementMethod.getInstance());
Spanned article = Html.fromHtml(text, null, null);
setHTML(article);
}
public void setHTML(Spanned html) {
SpannableString message = new SpannableString(html.toString());
Object[] spans = html.getSpans(0, html.length(), Object.class);
for (Object span : spans) {
int start = html.getSpanStart(span);
int end = html.getSpanEnd(span);
int flags = html.getSpanFlags(span);
if (span instanceof URLSpan) {
URLSpan urlSpan = (URLSpan) span;
span = new CallbackSpan(urlSpan.getURL());
}
message.setSpan(span, start, end, flags);
}
tv.setText(message);
}
public String explode(String str){
String pattern = "([a-zA-Z])";
str = str.replaceAll(pattern, "<a href=\"$0\">$0</a>");
return str;
}
private final class CallbackSpan extends ClickableSpan {
private String m_data;
private String url_main;
public CallbackSpan(String url) {
m_data = url.substring(0);
url_main = url;
}
public void onClick(View view) {
TextView item = (TextView)findViewById(R.id.tv2);
item.setText(url_main + " was clicked.");
Log.d("item", url_main);
}
}
}