我在弹出的对话框中添加了由[方括号]包围的文本的链接。但是,链接不可点击(按下时没有任何反应)。我无法弄明白为什么(!)
这是我的对话框活动:
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'在前面定义,如下所示:
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意图),但我需要确认在我达到这个目标之前发生了某种响应...
答案 0 :(得分: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]
并将其传递给另一个活动。
虽然我不喜欢因为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());
}
}
}
请注意,此方法不会删除每个关键字周围的[brackets]
,但您可以在while循环中轻松执行此操作。
要使用此功能,只需将TextView和Pattern传递给addLinks()
中的onCreate()
和 Voila!
适合您的工作示例:
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();
}
}
}