2012-09-11 83 views
0

我已经在弹出的对话框中添加了[方括号]包围的文本链接。但是,链接不可点击(按下时没有任何反应)。我不明白为什么Linkify() - 激活链接

这里是我的对话框活动(!):

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 
    } 

'计划' 和 '模式' 是前面定义如下:

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意图),但我需要确认某种响应正在发生之前,我才明白...

回答

3

我实际上并不想发动URL链接

既然你不需要使用的网址,不要试图创建一个自定义Linkify方案,因为它只会造成URLSpans打扰。你只是想从TextView中的关键字开始一个Activity。正如我在你的同类之一,但表示不重复,问题我会用一个自定义的范围,引入ActivitySpan:

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]您在包围括号并将它传递给另一个Activity。

虽然我不喜欢用,因为URLSpans的Linkify的想法,它的模式匹配和跨距创建是很大的,所以我复制并修改了它:

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

作为一个说明,这种方法没有按” t删除围绕每个关键字的[brackets],但您可以在while循环中轻松完成此操作。

要使用此功能,只需将TextView和Pattern传送到addLinks(),onCreate()即可!


为你工作的示例:

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

我输入文本当前字符串。我如何将其转换为TextView? – CaptainProg

+0

我真的不明白这个问题。您是否无法将Spannable传递给AlertDialog? – Sam

+0

我的意思是 - 我的输入文本进入addLinks()是一个字符串 - 我从一个HashMap中检索它。问题是我不能把一个字符串放到上面的解决方案中 - 我知道我必须在这里丢失一些基本的东西...... 据我所知,TextView是一个文本编辑器对象? – CaptainProg