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());
本文介绍如何在Android应用中创建一个包含链接的关于页面,并详细解释了设置TextView以支持链接点击及自动跳转的方法。
543

被折叠的 条评论
为什么被折叠?



