我已经加入到文本的链接是通过在弹出的对话框中[方括号]包围。然而,链接无法点击(当它们是pressed没有任何反应)。我不明白为什么(!)
下面是我的对话框活动:
公共无效popupDefinition(CharSequence的术语,LinkedHashMap的<字符串,字符串> dictionaryMap){ SpannableString定义=新SpannableString(dictionaryMap.get(项)); //通过对字典映射哈希检查抢定义 Linkify.addLinks(定义,模式,方案); //查找文本在方括号中,添加链接 AlertDialog alertDialog =新AlertDialog.Builder(ListProjectActivity.this).create(); //创建一个对话框 alertDialog.setMessage(definitionFormatted); //设置对话框消息 alertDialog.show(); //实际显示的对话框 }
计划和模式是早先定义如下:
最终静态模式模式= Pattern.compile(\\\\ [^] *]); //定义链接是由[方括号]约束的事实最后弦乐计划=http://example.com/; //这是不工作
为什么,当我点击出现的(它们出现在精美的蓝色下划线)的联系,他们不引起任何反应?
我不竟试图发起URL链接(我会重定向时确实发生它ACTION_VIEW意图),但我需要确认某种反应发生之前,我到...
解决方案我不竟试图发起URL链接
既然你不需要使用的网址,不要试图创建一个自定义Linkify方案,因为它只会造成URLSpans打扰。你只是想从一个TextView一个关键字开始一个活动。正如我在你的同类之一,但不可以重复,问题说我会用一个自定义的范围,引入ActivitySpan:
公共类ActivitySpan扩展ClickableSpan { 字符串关键字; 公共ActivitySpan(字符串关键字){ 超(); this.keyword =关键字; } @覆盖 公共无效的onClick(视图v){ 上下文的背景下= v.getContext(); 意向意图=新意图(背景下,AnotherActivity.class); intent.putExtra(关键词,关键词); context.startActivity(意向); }}
有没有铃铛或口哨这里,这跨度需要一个 [关键词]
您在括号括起来,并把它传递给另一个活动。
虽然我不喜欢用,因为URLSpans的Linkify的想法,它的模式匹配和跨距创建是伟大的,所以我复制和修改它:
私人无效addLinks(TextView的TextView的,图案花纹){ SpannableString spannable = SpannableString.valueOf(textView.getText()); 匹配匹配= pattern.matcher(spannable); //创建每场比赛ActivitySpans 而(matcher.find()) spannable.setSpan(新ActivitySpan(matcher.group()),matcher.start(),matcher.end(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //设置TextView的中新跨越 textView.setText(spannable); //监听spannable点击,如果尚未 MovementMethod M = textView.getMovementMethod(); 如果((M == NULL)||!(M的instanceof LinkMovementMethod)){ 如果(textView.getLinksClickable()){ textView.setMovementMethod(LinkMovementMethod.getInstance()); } }}
作为一个说明,这种方法并不能删除方括号[]
周围的每个关键字,但你可以很容易地做到这一点在while循环。
要使用此功能,只需通过一个TextView和模式,以 addLinks()
在的onCreate()
和瞧!的
为你工作的例子:
公共类示例扩展活动{ 模式模式= Pattern.compile(\\\\ [^] *]); @覆盖 公共无效的onCreate(捆绑savedInstanceState){ super.onCreate(savedInstanceState); popupDefinition(示例:[模式]或[产品型号],作为东西被[模仿]或[回避]); } //好像你可以称之为popupDefinition(dictionaryMap.get(项));而不是通过两者。 公共无效popupDefinition(字符串字符串){ SpannableString spannable =新SpannableString(字符串); 匹配匹配= pattern.matcher(spannable); //创建每场比赛ActivitySpans 而(matcher.find()) spannable.setSpan(新ActivitySpan(matcher.group()),matcher.start(),matcher.end(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //创建这些跨越了新的TextView并启用点击链接 TextView中的TextView =新的TextView(本); textView.setText(spannable); textView.setMovementMethod(LinkMovementMethod.getInstance()); //创建与该TextView中显示AlertDialog AlertDialog alertDialog =新AlertDialog.Builder(本) .setView(TextView中) 。创建(); alertDialog.show(); } 公共类ActivitySpan扩展ClickableSpan { 字符串关键字; 公共ActivitySpan(字符串关键字){ 超(); this.keyword =关键字; } @覆盖 公共无效的onClick(视图v){ 上下文的背景下= v.getContext(); Toast.makeText(上下文,关键字Toast.LENGTH_SHORT).show(); } }}
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...
解决方案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();
}
}
}