本文介绍了android中TextView内的可点击单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 TextView 带有动态更改的文本.此文本包含 <a href='myWord'>myWord</a> 之类的字符串.我希望在单击此链接"后 myWord 出现在同一活动的 EditText 中.

I have TextView with text that changed dynamically. This text contain strings like <a href='myWord'>myWord</a>. I want that after click to this "link" myWord appear in the EditText in the same activity.

这是我的代码:

txt.setText(Html.fromHtml("...<a href='link'>link</a>..."));
txt.setMovementMethod(LinkMovementMethod.getInstance());

对于 href 属性内的 URL 效果很好,但是对于另一种格式有错误.

It's work well for URLs inside href attribute, but there is an error for another format.

我在 StackOverflow 上发现了很多类似的问题,但都是关于 url 链接的.在我的应用程序中,我想在活动中创建链接".一般来说,如果它依赖,我可以将标签更改为另一个标签......

I found a lot of similar questions on the StackOverflow but all of them were about url links. In my app I want create "link" inside activity.In general, I can change tag to some another if it's depend...

请帮帮我!谢谢!

-----已解决-----感谢 Jacob Phillips 的想法!

-----SOLVED-----Thank you Jacob Phillips for idea!

希望将来有人会很有趣.这是一个代码:

May it will be interesting someone in future.This is a code:

//This is my string;
String str = "<b>Text</b> which contains one <a href='#'>link</a> and another <a href='#'>link</a>";
//TextView;
TextView txt = new TextView(this);
//Split string to parts:
String[] devFull = data[v.getId()][1].split("<a href='#'>");
//Adding first part:
txt.append(Html.fromHtml(devFull[0]));
//Creating array for parts with links (they amount always will devFull.length-1):
SpannableString[] link = new SpannableString[devFull.length-1];
//local vars:
ClickableSpan[] cs = new ClickableSpan[devFull.length-1];
String linkWord;
String[] devDevFull = new String[2];

for(int i=1; i<devFull.length; i++){
    //obtaining 'clear' link
    devDevFull = devFull[i].split("</a>");
    link[i-1] = new SpannableString(devDevFull[0]);
    linkWord = devDevFull[0];
    cs[i-1] = new ClickableSpan(){
        private String w = linkWord;
        @Override
        public void onClick(View widget) {
            // here you can use w (linkWord)
        }
    };
    link[i-1].setSpan(cs[i-1], 0, linkWord.length(), 0);
    txt.append(link[i-1]);
    try{
        txt.append(Html.fromHtml(devDevFull[1]));
    }
    catch(Exception e){}
}

推荐答案

这应该可以解决问题.只需在 OnClickListener 中更改您的 edittext 文本.它可能可以减少,但这应该有效.

This should do the trick. Just change your edittext's text in the OnClickListener. It may be able to be reduced but this should work.

private void foo() {
    SpannableString link = makeLinkSpan("click here", new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // respond to click
        }
    });

    // We need a TextView instance.
    TextView tv = new TextView(context);

    // Set the TextView's text
    tv.setText("To perform action, ");

    // Append the link we created above using a function defined below.
    tv.append(link);

    // Append a period (this will not be a link).
    tv.append(".");

    // This line makes the link clickable!
    makeLinksFocusable(tv);
}

/*
 * Methods used above.
 */

private SpannableString makeLinkSpan(CharSequence text, View.OnClickListener listener) {
    SpannableString link = new SpannableString(text);
    link.setSpan(new ClickableString(listener), 0, text.length(),
        SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
    return link;
}

private void makeLinksFocusable(TextView tv) {
    MovementMethod m = tv.getMovementMethod();
    if ((m == null) || !(m instanceof LinkMovementMethod)) {
        if (tv.getLinksClickable()) {
            tv.setMovementMethod(LinkMovementMethod.getInstance());
        }
    }
}

/*
 * ClickableString class
 */

private static class ClickableString extends ClickableSpan {
    private View.OnClickListener mListener;
    public ClickableString(View.OnClickListener listener) {
        mListener = listener;
    }
    @Override
    public void onClick(View v) {
        mListener.onClick(v);
    }
}

这篇关于android中TextView内的可点击单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-18 03:38