如何在Textview Clickable中创建特定单词

时间:2015-06-02 14:12:48

标签: android textview clickable

我有:

String desc="Drugs in this section include antimuscarinic compounds (section 4.3) and drugs believed to be direct relaxants of intestinal smooth muscle. The smooth muscle relaxant properties of antimuscarinic(section 4.4) and other antispasmodic drugs may be useful in irritable bowel syndrome(section 4.5) and in diverticular disease.The dopamine-receptor antagonists metoclopramide and domperidone (section 4.6) stimulate transit in the gut.";

我想使用不同的点击目标制作(section 4.3)(section 4.4)(section 4.5)(section 4.6) 可点击

我该如何实施?

2 个答案:

答案 0 :(得分:1)

使用html实现链接: Android: textview hyperlink

使用方案意图过滤器来处理链接点击: How to implement my very own URI scheme on Android

答案 1 :(得分:1)

您可以使用ClickableSpan找到这样的括号:

TextView myTextView = new TextView(this);
String myString = "Drugs in this section include antimuscarinic compounds (section 4.3)";
int i1 = myString.indexOf("(");
int i2 = myString.indexOf(")");
myTextView.setMovementMethod(LinkMovementMethod.getInstance());
myTextView.setText(myString, BufferType.SPANNABLE);
Spannable mySpannable = (Spannable)myTextView.getText();
ClickableSpan myClickableSpan = new ClickableSpan()
{
 @Override
 public void onClick(View widget) { /* do something */ }
};
mySpannable.setSpan(myClickableSpan, i1, i2 + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);