我在尝试一些简单的东西。在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());
在我的 androidmanifest .xml我添加了intent -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
怎么了?
你需要调用用户的web浏览器来打开它,或者调用webview来打开应用程序。你没有任何代码来覆盖它。
WebView webview = new WebView(this);
setContentView(webview);
webview.loadUrl(www.google.com);
以上是在应用程序。用于拉出用户web客户端:
getWindow().requestFeature(Window.FEATURE_PROGRESS);
webview.getSettings().setJavaScriptEnabled(true);
final Activiy activity = this;
webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
activity.setProgress(progress * 1000);
}
});
webview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, "Oh no! " + description,
Toast.LENGTH_SHORT).show();
}
});
webview.loadUrl("www.google.com");
如果我使用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());
这就是几乎我想要实现的…但是链接没有正确呈现…(
这是它打印到AlertDialog中的结果:
& lt;a href="http://www.google.com">查看此链接
当然,它应该显示它没有HTML标签,像这样->检查这个链接…
所以这是最终的解决方案在alertdialog中获得格式良好的超链接而没有任何错误:
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时,生成的URL将是错误的。addLinks(TextView TextView, Pattern Pattern, String scheme).
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 "";
}
});
}
所以,这真的很舒服!:)