问题
I have added links to text that is surrounded by [square brackets] in a popup dialog box. The links however, are not clickable (nothing happens when they are pressed). I can't work out why(!)
Here is my dialog box activity:
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' and 'pattern' are defined earlier, as follows:
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
Why, when I tap the links that appear (they appear nicely underlined in blue), do they not cause any response?
I'm not actually trying to launch URL links (I'll be redirecting the ACTION_VIEW intent when it does occur), but I need to confirm that some sort of response is happening before I get to that...
回答1:
I'm not actually trying to launch URL links
Since you don't need to use URLs, don't bother with trying to create a custom Linkify scheme since it only creates URLSpans. You simply want to start an Activity from a keyword in a TextView. As I stated in one of your similar, but not duplicate, questions I would use a custom span, introducing 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);
}
}
There are no bells or whistles here, this span takes a [keyword]
that you surrounded in brackets and passes it to the another Activity.
While I don't like the idea of using Linkify because of URLSpans, its pattern matching and span creation is great, so I copied and modified it:
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());
}
}
}
As a note, this method doesn't remove the [brackets]
surrounding each keyword, but you can easily do this in the while-loop.
To use this, simply pass a TextView and Pattern to addLinks()
in onCreate()
and Voila!
A working example for you:
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();
}
}
}
来源:https://stackoverflow.com/questions/12361059/linkify-activating-links