最近有一个需求,在一个Dialog中显示一个带有链接的文本,点击这个链接文本跳转到指定的网站中,同时这个Dialog消失。具体的实现如下:
用到的Dialog的contentView的资源文件dlg_with_link_text.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/forward_to_ygc87_csdn_blog_title" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="20dip" android:gravity="center_horizontal" android:text="@string/forward_to_ygc87_csdn_blog_title" android:textAppearance="?android:attr/textAppearanceMedium" android:textSize="20sp" /> <TextView android:id="@+id/link_text_view" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginBottom="40dip" android:layout_marginTop="40dip" android:gravity="center_horizontal" android:textAppearance="?android:attr/textAppearanceSmall" android:textSize="15sp" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">ygc87CSDNblog</string> <string name="app_name">ygc87CSDNblog</string> <string name="forward_to_ygc87_csdn_blog_title">ygc87的CSDN博客</string> <string name="forward_to_ygc87_csdn_blog"><a href=\"http://blog.csdn.net/ygc87\">点击这里</a>跳转到ygc87的CSDN博客!</string> <string name="confirm_know">知道了</string> </resources>
import android.app.Activity; import android.app.AlertDialog; import android.os.Bundle; import android.text.Html; import android.text.Spannable; import android.text.SpannableStringBuilder; import android.text.method.LinkMovementMethod; import android.text.style.URLSpan; import android.view.LayoutInflater; import android.view.View; import android.widget.TextView; public class TextViewLinkTextClickListener extends Activity { private AlertDialog mAlertDialog; private class MyURLSpan extends URLSpan { public MyURLSpan(String url) { super(url); } @Override public void onClick(View widget) { //调用super方法实现点击LinkText后跳转到指定网页 super.onClick(widget); //之所以要监听LinkText点击事件是为了点击LinkText后关闭Dlg if (mAlertDialog != null) { mAlertDialog.dismiss(); mAlertDialog = null; } } } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); showDlgWithLinkText(); } private void showDlgWithLinkText() { View dlgContentView = LayoutInflater.from(this).inflate(R.layout.dlg_with_link_text, null); //设置字体加粗 ((TextView) dlgContentView.findViewById(R.id.forward_to_ygc87_csdn_blog_title)).getPaint().setFakeBoldText(true); TextView forwardToXiaoMiLogin = (TextView) dlgContentView.findViewById(R.id.link_text_view); //要通过Html.fromHtml()方法转换带有LinkText的文本,然后将文本强转为Spannable类型 Spannable textWithLinkText = (Spannable) Html.fromHtml(getString(R.string.forward_to_ygc87_csdn_blog)); //获取文本中原有的URLSpan类型的文本,保存起来 int end = textWithLinkText.length(); URLSpan[] urls = textWithLinkText.getSpans(0, end, URLSpan.class); //使用textWithLinkText创建一个SpannableStringBuilder,通过clearSpans()方法清除原有的Span SpannableStringBuilder style = new SpannableStringBuilder(textWithLinkText); style.clearSpans(); //重新设置textWithLinkText中的URLSpan for (URLSpan url : urls) { MyURLSpan myURLSpan = new MyURLSpan(url.getURL()); style.setSpan(myURLSpan, textWithLinkText.getSpanStart(url), textWithLinkText.getSpanEnd(url), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } //将重新设置Span的文本设置为forwardToXiaoMiLogin的显示文字 forwardToXiaoMiLogin.setText(style); //通过setMovementMethod设置LinkMovementMethod类型来使LinkText有效 forwardToXiaoMiLogin.setMovementMethod(LinkMovementMethod.getInstance()); mAlertDialog = new AlertDialog.Builder(this).setView(dlgContentView).setNegativeButton(R.string.confirm_know, null).show(); } }
效果如下:
点击链接后:
返回: