Android 在TextView中使用AutoLink,并自定义点击链接后的行为
来源:互联网 发布:龙华数据恢复 编辑:程序博客网 时间:2024/05/18 02:03
原文链接:http://www.artinapp.com/blog/2012/10/30/android-%e5%9c%a8textview%e4%b8%ad%e4%bd%bf%e7%94%a8autolink%ef%bc%8c%e5%b9%b6%e8%87%aa%e5%ae%9a%e4%b9%89%e7%82%b9%e5%87%bb%e9%93%be%e6%8e%a5%e5%90%8e%e7%9a%84%e8%a1%8c%e4%b8%ba/
package com.carey.common.textautolink;import com.zyx.testb.R;import android.app.Activity;import android.app.AlertDialog;import android.content.Context;import android.content.DialogInterface;import android.os.Bundle;import android.text.Spannable;import android.text.SpannableStringBuilder;import android.text.style.ClickableSpan;import android.text.style.URLSpan;import android.text.util.Linkify;import android.view.View;import android.widget.TextView;import android.widget.Toast;public class AutoLinkActivity extends Activity { private TextView tvLinked = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_testautolink); tvLinked = (TextView) findViewById(R.id.linkedText); // set the TextView's link mask, auto link all. tvLinked.setAutoLinkMask(Linkify.ALL); // we have three links in this sentence tvLinked.setText("http://www.baidu.com what is your website, 123456456 mine is www.hellokitty.com wish like it. thanks!!"); CharSequence content = tvLinked.getText(); SpannableStringBuilder builder = SpannableStringBuilder.valueOf(content); // listen and get the spans URLSpan[] spans = builder.getSpans(0, content.length(), URLSpan.class); if (spans != null && spans.length > 0) { int start = 0; int end = 0; for (URLSpan span : spans) { start = builder.getSpanStart(span); end = builder.getSpanEnd(span); // to replace each link span with customized ClickableSpan builder.removeSpan(span); builder.setSpan(new MyURLSpan(AutoLinkActivity.this, span.getURL().toString()), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } }
<pre name="code" class="java"> // set the TextView's link mask, no auto link .tvLinked.setAutoLinkMask(0); tvLinked.setText(builder); } class MyURLSpan extends ClickableSpan { private Context mContext = null; private String mUrl = ""; public MyURLSpan(Context context, String url) { this.mContext = context; this.mUrl = url; } @Override public void onClick(View widget) { AlertDialog.Builder builder = new AlertDialog.Builder(mContext); builder.setTitle("Sure ?").setMessage("Confirm?") .setPositiveButton("Ok", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // do some customization, like open your own WebView // within your app. Toast.makeText(AutoLinkActivity.this, "you can do anything you like here", Toast.LENGTH_LONG).show(); } }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(AutoLinkActivity.this, "you can do anything you like here", Toast.LENGTH_LONG).show(); } }).create().show(); } }}
activity_testautolink.xml
<pre name="code" class="html"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/linkedText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="hello_world" tools:context=".MainActivity" /></LinearLayout>
</pre><pre name="code" class="java">
0 0
- Android 在TextView中使用AutoLink,并自定义点击链接后的行为
- TextView中android:autoLink属性的作用
- Android TextView中添加链接并在链接部分字体变色可点击
- TextView中autolink的下划线
- 关于在listview中加textview的autolink的问题
- android 自定义TextView支持微博功能后在ListView占用了Item点击的解决办法
- android 自定义TextView支持微博功能后在ListView占用了Item点击的解决办法
- android 自定义TextView支持微博功能后在ListView占用了Item点击的解决办法
- 在TextView中自定义链接
- 自定义可点击的ImageSpan并在TextView中内置“View“
- 自定义可点击的ImageSpan并在TextView中内置“View“
- 自定义可点击的ImageSpan并在TextView中内置“View“
- 点击TextView中链接,自定义处理事件
- TextView中autoLink属性的设置
- 【android】短信中包含链接的textview的点击处理
- TextView的autoLink属性
- TextView的autoLink属性
- Android TextView中链接(link)点击事件的截取
- kernel日志buff 大小修改
- 这是在CSDN的第一篇博文
- 2G到4G网络结构的演进过程(图)
- 关于单例设计模式的总结
- 去掉std::string或std::wstring的最后一个字符的简单方法
- Android 在TextView中使用AutoLink,并自定义点击链接后的行为
- ResultSet用法集锦
- java shiro、maven、Bootstrap、HTML5、SpringMVC、Mybatis、Hibernate、安全权限、高性能、高并发
- Android自定义控件(二)——圆形进度条以及回调方法
- The content of the adapter has changed but ListView did not receive a notification
- php上传文件大小限制解决方法
- JAVA连接MYSQL配置
- CSS实现自适应圆角矩阵——滑动门
- wstring,WCHAR,TCHAR,CHAR