首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Linkify() -激活链接

Linkify() -激活链接
EN

Stack Overflow用户
提问于 2012-09-11 08:08:21
回答 1查看 1.8K关注 0票数 0

我已经在弹出对话框中添加了指向由方括号包围的文本的链接。然而,这些链接是不可点击的(当它们被按下时没有任何反应)。我想不出为什么(!)

下面是我的对话框活动:

代码语言:javascript
复制
public void popupDefinition(CharSequence term, LinkedHashMap<String, String> dictionaryMap){
    SpannableString definition = new SpannableString(dictionaryMap.get(term)); // grab the definition by checking against the dictionary map hash
    Linkify.addLinks(definition, pattern, scheme); // find text in square brackets, add links

    AlertDialog alertDialog = new AlertDialog.Builder(ListProjectActivity.this).create(); // create a dialog box
    alertDialog.setMessage(definitionFormatted); // set dialog box message
    alertDialog.show(); // actually display the dialog box
    }

‘'scheme’和'pattern‘在前面已经定义好了,如下所示:

代码语言:javascript
复制
final static Pattern pattern = Pattern.compile("\\[[^]]*]"); // defines the fact that links are bound by [square brackets]
final String scheme = "http://example.com/"; // THIS IS NOT WORKING

为什么,当我点击出现的链接(它们以蓝色下划线显示)时,它们没有引起任何响应?

我实际上并没有尝试启动URL链接(当它发生时,我将重定向ACTION_VIEW意图),但我需要在开始之前确认是否发生了某种响应……

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-09-12 02:54:02

我实际上并不是在尝试启动

链接

由于您不需要使用URLSpans,因此不需要尝试创建自定义链接方案,因为它只创建URL。您只需要从TextView中的关键字启动一个活动。正如我在你的一个类似的,但不是复制的问题中所说的,我会使用一个自定义的跨度,介绍ActivitySpan:

代码语言:javascript
复制
public class ActivitySpan extends ClickableSpan {
    String keyword;
    public ActivitySpan(String keyword) {
        super();
        this.keyword = keyword;
    }

    @Override
    public void onClick(View v) {
        Context context = v.getContext();
        Intent intent = new Intent(context, AnotherActivity.class);
        intent.putExtra("keyword", keyword);
        context.startActivity(intent);
    }
}

这里没有铃声或口哨,这个跨度接受一个你用括号括起来的[keyword],并将它传递给另一个活动。

虽然我不喜欢因为URLSpans而使用Linkify的想法,但它的模式匹配和跨度创建非常棒,所以我复制并修改了它:

代码语言:javascript
复制
private void addLinks(TextView textView, Pattern pattern) {
    SpannableString spannable = SpannableString.valueOf(textView.getText());
    Matcher matcher = pattern.matcher(spannable);

    // Create ActivitySpans for each match
    while (matcher.find())
        spannable.setSpan(new ActivitySpan(matcher.group()), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    // Set new spans in TextView
    textView.setText(spannable);

    // Listen for spannable clicks, if not already
    MovementMethod m = textView.getMovementMethod();
    if ((m == null) || !(m instanceof LinkMovementMethod)) {
        if (textView.getLinksClickable()) {
            textView.setMovementMethod(LinkMovementMethod.getInstance());
        }
    }
}

注意,此方法不会删除每个关键字周围的[brackets],但您可以在while循环中轻松完成此操作。

要使用它,只需在onCreate()和Voila中将TextView和模式传递给addLinks()

下面是一个实用的示例:

代码语言:javascript
复制
public class Example extends Activity {
    Pattern pattern = Pattern.compile("\\[[^]]*]");

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        popupDefinition("Example: A [pattern] or [model], as of something to be [imitated] or [avoided]");
    }

    // It seems like you can call "popupDefinition(dictionaryMap.get(term));" rather than pass both.  
    public void popupDefinition(String string){
        SpannableString spannable = new SpannableString(string);
        Matcher matcher = pattern.matcher(spannable);

        // Create ActivitySpans for each match
        while (matcher.find())
            spannable.setSpan(new ActivitySpan(matcher.group()), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        // Create a new TextView with these spans and enable the clickable links
        TextView textView = new TextView(this);
        textView.setText(spannable);
        textView.setMovementMethod(LinkMovementMethod.getInstance());

        // Create and display an AlertDialog with this TextView
        AlertDialog alertDialog = new AlertDialog.Builder(this)
                .setView(textView)
                .create(); 
        alertDialog.show(); 
    }


    public class ActivitySpan extends ClickableSpan {
        String keyword;
        public ActivitySpan(String keyword) {
            super();
            this.keyword = keyword;
        }

        @Override
        public void onClick(View v) {
            Context context = v.getContext();
            Toast.makeText(context, keyword, Toast.LENGTH_SHORT).show();
        }
    }
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12361059

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档