在dialog中使用超链接

本文介绍如何在Android应用中创建一个包含链接的关于页面,并详细解释了设置TextView以支持链接点击及自动跳转的方法。
View view = View.inflate(MainActivity.this, R.layout.about, null); 
TextView textView = (TextView) view.findViewById(R.id.message); 
textView.setMovementMethod(LinkMovementMethod.getInstance()); 
textView.setText(R.string.Text_About); 
new AlertDialog.Builder(MainActivity.this).setTitle( 
        R.string.Title_About).setView(view) 
        .setPositiveButton(android.R.string.ok, null) 
        .setIcon(R.drawable.icon).show(); 

 about.xml

 

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/scrollView" android:layout_width="fill_parent" 
    android:layout_height="wrap_content" android:paddingTop="2dip" 
    android:paddingBottom="12dip" android:paddingLeft="14dip" 
    android:paddingRight="10dip"> 
    <TextView android:id="@+id/message" style="?android:attr/textAppearanceMedium" 
        android:layout_width="fill_parent" android:layout_height="wrap_content" 
        android:padding="5dip" android:linksClickable="true" /> 
</ScrollView> 

 

这里要注意两个地方

linksClickable 为 true 必须setMovementMethod(LinkMovementMethod.getInstance()).

上面的方式不能直接跳转,只是可以用来点击。

而如果要跳转

则应该加上android:autoLink="web"

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:paddingLeft="2dip" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical">
  <!-- 
  <TextView android:id="@+id/ABOUT_NAME" android:text="@string/app_name" android:layout_width="wrap_content" android:layout_height="wrap_content" /> 
  <TextView android:id="@+id/ABOUT_VERSION" android:text="@string/app_name" android:layout_width="wrap_content" android:layout_height="wrap_content" /> 
  
  <TextView android:id="@+id/ABOUT_URL" android:text="www.google.cn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:autoLink="web" />
   -->
  <TextView android:id="@+id/ABOUT_COPYRIGHT"  android:text="www.google.cn" android:linksClickable="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:autoLink="web"/> 
 
 </LinearLayout>

 

new AlertDialog.Builder(debugApp.this) 
    	.setTitle(R.string.hello) 
    	.setView(view).show();

 

public static class MyOtherAlertDialog { 
 
 public static AlertDialog create(Context context) { 
  final TextView message = new TextView(context); 
  // i.e.: R.string.dialog_message => 
            // "Test this dialog following the link to dtmilano.blogspot.com" 
  final SpannableString s =  
               new SpannableString(context.getText(R.string.dialog_message)); 
  Linkify.addLinks(s, Linkify.WEB_URLS); 
  message.setText(s); 
  message.setMovementMethod(LinkMovementMethod.getInstance()); 
 
  return new AlertDialog.Builder(context) 
   .setTitle(R.string.dialog_title) 
   .setCancelable(true) 
   .setIcon(android.R.drawable.ic_dialog_info) 
   .setPositiveButton(R.string.dialog_action_dismiss, null) 
   .setView(message) 
   .create(); 
 } 
} 

  

6.

 final SpannableString stMyWeb = new SpannableString("http://android-er.blogspot.com/");  
        Linkify.addLinks(stMyWeb, Linkify.ALL);   
        final AlertDialog aboutDialog = new AlertDialog.Builder(testLayout.this).setMessage(stMyWeb).setPositiveButton("OK", new DialogInterface.OnClickListener() { 
        	@Override
        	public void onClick(DialogInterface dialog, int which) {  
        		// TODO Auto-generated method stub  
        		}}).create();   
        	aboutDialog.show();
        	((TextView)aboutDialog.findViewById(android.R.id.message)) .setMovementMethod(LinkMovementMethod.getInstance());

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值