介绍
每次我们点击微博那个#内容#的时候,我们会如何去想这个是什么原理呢?从android移动端口去考虑。
那么这个可能是如何去实现的呢?
从理论上有几种的实现方式,第一个是HTML的实现,TextView控件中可插入html 的文本内容,在html文本中显示这个超链接。但?后台是很忙碌的?有什么可以简单的减轻后台的工作量呢?
那么就可能需要用到ClickableSpan这个接口~~
对了,文中的demo例子是属于android simpledemo ,我只是对某些内容进行深入~~
先贴代码
public class TextViewMainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.textviewmainactivity);
init();
}
private void init() {
TextView textViewResource = (TextView) findViewById(R.id.text_html_resource);
textViewResource.setText(
Html.fromHtml(getResources().getString(R.string.link_text_manual)));
textViewResource.setMovementMethod(LinkMovementMethod.getInstance());
//
TextView textViewHtml = (TextView) findViewById(R.id.text_html_program);
textViewHtml.setText(
Html.fromHtml(
"<b>text_html_program: Constructed from HTML programmatically.</b>"
+ " Text with a <a href=\"http://www.google.com\">link</a> "
+ "created in the Java source code using HTML."));
textViewHtml.setMovementMethod(LinkMovementMethod.getInstance());
//
SpannableString ss = new SpannableString(
"text_spannable: Manually created spans. Click here to dial the phone.");
ss.setSpan(new StyleSpan(Typeface.BOLD), 0, 39,
Spanned.SPAN_INCLUSIVE_INCLUSIVE);
ss.setSpan(new URLSpan("tel:4155551212"), 40 + 6, 40 + 10,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//
TextView textViewSpan = (TextView) findViewById(R.id.text_spannable);
String string = "#hello#what is yout ##name ,my name is #Yello#";
SpannableString spStr = new SpannableString(string);
Pattern pattern = Pattern.compile("#[^#]+#");
Matcher matcher = pattern.matcher(string);
boolean result = matcher.find();
if (result) {
do {
MyClickSpan clickSpan = new MyClickSpan(this,matcher.group()); //设置超链接
spStr.setSpan(clickSpan, matcher.start(), matcher.end(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}while (matcher.find());
textViewSpan.append(spStr);
textViewSpan.setMovementMethod(LinkMovementMethod.getInstance());
} else {
textViewSpan.setText("匹配失败");
}
}
private class MyClickSpan extends ClickableSpan {
private Context context;
private String string;
public MyClickSpan(Context context,String string) {
super();
this.context = context;
this.string = string;
}
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setColor(ds.linkColor);
ds.setUnderlineText(false);
}
@Override
public void onClick(View widget) {
// Intent intent = new Intent(context, MainActivity.class);
// startActivity(intent);
Toast.makeText(context,string,Toast.LENGTH_LONG).show();
}
}
}
然后是布局文件哦~~~
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
style="@style/LinkText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/intro" />
<!-- text_auto_linkify automatically linkifies things like URLs and phone numbers. -->
<TextView
android:id="@+id/text_auto_linkify"
style="@style/LinkText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autoLink="all"
android:text="@string/link_text_auto" />
<!--
text_html_resource uses a string resource containing explicit anchor tags (<a>)
to specify links.
-->
<TextView
android:id="@+id/text_html_resource"
style="@style/LinkText"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- text_html_program builds the text in the Java code using HTML. -->
<TextView
android:id="@+id/text_html_program"
style="@style/LinkText"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- text_spannable builds the text in the Java code without using HTML. -->
<TextView
android:id="@+id/text_spannable"
style="@style/LinkText"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
布局文件之后是字符文件~~这个也是很重要的
<resources>
<string name="app_name">test1</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="intro">This sample illustrates how links can be added to a TextView.
\nThis can be done either automatically by setting the <i>autoLink</i> property
or explicitly.</string>
<string name="link_text_auto"><b>text_auto_linkify: Various kinds
of data that will be auto-linked.</b>
In this text are some things that are actionable. For instance,
you can click on http://www.google.com and it will launch the
web browser. You can click on google.com too. If you
click on (415) 555-1212 it should dial the phone. Or just write
foobar@example.com for an e-mail link. If you have a URI like
http://www.example.com/lala/foobar@example.com you should get the
full link not the e-mail address. Or you can put a location
like 1600 Amphitheatre Parkway, Mountain View, CA 94043. To summarize:
https://www.google.com, or 650-253-0000, somebody@example.com,
or 9606 North MoPac Expressway, Suite 400, Austin, TX 78759.</string>
<string name="link_text_manual"><![CDATA[<b>text_html_resource:
Explicit links using <a> markup.</b>
This has markup for a <a href="http://www.google.com">link</a> specified
via an <a> tag. Use a \"tel:\" URL
to <a href="tel:4155551212">dial a phone number</a>.]]></string>
</resources>
重点
在这个小demo中,重要的是写好匹配字符的那个东西……^$的具体含义最好了解清楚,然后自己做一下实验。不然会被坑到飞起……把点击事件声明在 MyClickSpan 中,然后点击文字就会有对应的事件产生。
图片的预览