Android Linkify()-激活链接

Android Linkify()-激活链接,android,linkify,Android,Linkify,我在弹出对话框中添加了指向被[方括号]包围的文本的链接。然而,链接是不可点击的(按下链接时不会发生任何事情)。我不明白为什么(!) 以下是我的对话框活动: public void popupDefinition(CharSequence term, LinkedHashMap<String, String> dictionaryMap){ SpannableString definition = new SpannableString(dictionaryMap.get(te

我在弹出对话框中添加了指向被[方括号]包围的文本的链接。然而,链接是不可点击的(按下链接时不会发生任何事情)。我不明白为什么(!)

以下是我的对话框活动:

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
    }
为什么,当我点击出现的链接(它们用蓝色很好地加了下划线)时,它们不会引起任何响应

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

我实际上并没有尝试启动URL链接

因为您不需要使用URL,所以不必费心创建自定义Linkify方案,因为它只创建URLspan。您只需要从TextView中的关键字启动活动。正如我在你的一个类似但不重复的问题中所述,我将使用自定义span,介绍活动span:

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);
    }
}
这里没有铃铛或口哨,这个跨度采用括号中的
[关键字]
,并将其传递给另一个活动

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

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());
        }
    }
}
请注意,此方法不会删除每个关键字周围的
[括号]
,但您可以在while循环中轻松完成此操作

要使用它,只需在
onCreate()
中向
addLinks()
传递一个文本视图和模式,瞧


您的一个工作示例:

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();
        }
    }
}
我实际上并没有尝试启动URL链接

因为您不需要使用URL,所以不必费心创建自定义Linkify方案,因为它只创建URLspan。您只需要从TextView中的关键字启动活动。正如我在你的一个类似但不重复的问题中所述,我将使用自定义span,介绍活动span:

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);
    }
}
这里没有铃铛或口哨,这个跨度采用括号中的
[关键字]
,并将其传递给另一个活动

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

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());
        }
    }
}
请注意,此方法不会删除每个关键字周围的
[括号]
,但您可以在while循环中轻松完成此操作

要使用它,只需在
onCreate()
中向
addLinks()
传递一个文本视图和模式,瞧


您的一个工作示例:

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();
        }
    }
}

我的输入文本当前是一个字符串。如何将其转换为文本视图?我不太明白这个问题。将Spanable传递到AlertDialog时遇到问题吗?我的意思是-进入addLinks()的输入文本是字符串-我从HashMap检索它。问题是我不能在上面的解决方案中加入字符串-我理解我一定是缺少了一些基本的东西。。。据我所知,TextView是一个文本编辑器对象?只需在
addLinks()
的第一行将字符串传递给
SpannableString.valueOf()
。为了向遇到此问题的其他人澄清,我无法单击链接的原因是因为我使用的是setMessage(),而不是setView()。设置为消息将删除文本链接除了看起来漂亮之外的任何功能。我的输入文本当前为字符串。如何将其转换为文本视图?我不太明白这个问题。将Spanable传递到AlertDialog时遇到问题吗?我的意思是-进入addLinks()的输入文本是字符串-我从HashMap检索它。问题是我不能在上面的解决方案中加入字符串-我理解我一定是缺少了一些基本的东西。。。据我所知,TextView是一个文本编辑器对象?只需在
addLinks()
的第一行将字符串传递给
SpannableString.valueOf()
。为了向遇到此问题的其他人澄清,我无法单击链接的原因是因为我使用的是setMessage(),而不是setView()。设置为消息将删除文本链接除了看起来漂亮之外的任何功能。