我尝试过简单的事情。在AlertDialog消息中包含超链接。我可以包含超链接,并根据需要绘制为超链接。但是当点击它时,应用程序崩溃了:
ActivityMain.java
AlertDialog ad = new AlertDialog.Builder(this).create();
ad.setMessage(Html.fromHtml("<a href=\\\"http://www.google.com\\\">Check this link out</a>));
ad.setButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
ad.show();
TextView textView = (TextView) ad.findViewById(android.R.id.message);
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
textView.setMovementMethod(LinkMovementMethod.getInstance());
在我的 AndroidManifext.xml 中,我添加了intend-filter android.intent.action.VIEW ,但它仍然崩溃。
<activity
android:name="org.app.test"
android:screenOrientation="portrait"
android:label="@string/app_name"
android:theme="@style/AppTheme"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Logcat错误消息
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=\"http://www.google.com\" (has extras) }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1672)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1442)
at android.app.Activity.startActivityForResult(Activity.java:3511)
at de.robv.android.xposed.XposedBridge.invokeOriginalMethodNative(Native Method)
at de.robv.android.xposed.XposedBridge.handleHookedMethod(XposedBridge.java:631)
at android.app.Activity.startActivityForResult(Native Method)
at android.app.Activity.startActivityForResult(Activity.java:3472)
at de.robv.android.xposed.XposedBridge.invokeOriginalMethodNative(Native Method)
at de.robv.android.xposed.XposedBridge.handleHookedMethod(XposedBridge.java:631)
at android.app.Activity.startActivityForResult(Native Method)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:748)
at android.app.Activity.startActivity(Activity.java:3714)
at de.robv.android.xposed.XposedBridge.invokeOriginalMethodNative(Native Method)
at de.robv.android.xposed.XposedBridge.handleHookedMethod(XposedBridge.java:631)
at android.app.Activity.startActivity(Native Method)
at android.app.Activity.startActivity(Activity.java:3682)
at de.robv.android.xposed.XposedBridge.invokeOriginalMethodNative(Native Method)
at de.robv.android.xposed.XposedBridge.handleHookedMethod(XposedBridge.java:631)
at android.app.Activity.startActivity(Native Method)
at android.content.ContextWrapper.startActivity(ContextWrapper.java:323)
at de.robv.android.xposed.XposedBridge.invokeOriginalMethodNative(Native Method)
at de.robv.android.xposed.XposedBridge.handleHookedMethod(XposedBridge.java:631)
at android.content.ContextWrapper.startActivity(Native Method)
at android.text.style.URLSpan.onClick(URLSpan.java:62)
at android.text.method.LinkMovementMethod.onTouchEvent(LinkMovementMethod.java:217)
at android.widget.TextView.onTouchEvent(TextView.java:9180)
at android.view.View.dispatchTouchEvent(View.java:8107)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2411)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2144)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2411)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2144)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2411)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2144)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2411)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2144)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2411)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2144)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2411)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2144)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2280)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1615)
at android.app.Dialog.dispatchTouchEvent(Dialog.java:757)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2228)
at android.view.View.dispatchPointerEvent(View.java:8315)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4596)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4464)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4022)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4076)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4045)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4156)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4053)
at android.view.ViewRootImpl$Async
出了什么问题?
答案 0 :(得分:0)
如果我使用Linkify,可以无误地点击链接,并在网络浏览器中打开。
String msg = "<a href=\"http://www.google.com\">Check this link out</a>";
final SpannableString s = new SpannableString(msg);
Linkify.addLinks(s, Linkify.ALL);
AlertDialog ad = new AlertDialog.Builder(this)
.setMessage(s)
.setTitle("Click a link in AlertDialog")
.setIcon(android.R.drawable.ic_dialog_info)
.create();
TextView textView = (TextView) ad.findViewById(android.R.id.message);
textView.setMovementMethod(LinkMovementMethod.getInstance());
那是 ALMOST 我想要实现的......但是链接没有正确呈现...... :(
这是打印到我的AlertDialog中的结果:
&LT; a href = \“http://www.google.com \”&gt;检查此链接&lt; / A&GT;
当然它应该显示没有HTML标签,像这样 - &gt; Check this link out ......
所以这是最终的解决方案,以便在AlertDialogs中获得良好格式化的超链接,而不会出现任何错误:
String msg = "Check this link out, and I will send you to Google.";
AlertDialog ad = new AlertDialog.Builder(this)
.setTitle("Open a Hyperlink inside AlertDialog")
.setMessage(msg)
.setIcon(android.R.drawable.ic_dialog_info)
.create();
ad.show();
TextView message = (TextView) ad.findViewById(android.R.id.message);
textView.setMovementMethod(LinkMovementMethod.getInstance());
addLinks(message , "Check this link out", "http://www.google.com");
我们唯一需要做的就是在我们的类中添加一个小的新函数 addLinks(Textview,String,String)。否则,当我们只使用Linkify.addLinks(TextView textView,Pattern pattern,String scheme)时,生成的URL将是错误的。
public static void addLinks(TextView textView, String linkThis, String toThis)
{
Pattern pattern = Pattern.compile(linkThis);
String scheme = toThis;
Linkify.addLinks(textView, pattern, scheme, new Linkify.MatchFilter() {
@Override
public boolean acceptMatch(CharSequence s, int start, int end) {
return true;
}
}, new Linkify.TransformFilter() {
@Override
public String transformUrl(Matcher match, String url) {
return "";
}
});
}
所以,这让它真的很舒服! :)