java - Android:在自定义 Web View 中检测并打开外部链接

标签 java android android-fragments android-activity webview

我遇到了一个奇怪的问题,我费了九牛二虎之力还是解决不了。 问题是: 我有一个直接从 Twitter 加载数据的 textview。推文可能包含许多链接,它会显示在 TextView 中,但在单击链接时,它们会在 android 默认浏览器 中打开。我想要做的是当用户单击任何链接时我将如何检测哪个链接已被单击打开我的 webview 而不是默认浏览器

这是我到目前为止所达到的:

    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello www.google.com Thank You"
    android:autoLink="web"
    android:id="@+id/activity_main_textview"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

Java

TextView textView = (TextView) findViewById(R.id.activity_main_textview);
textView.setText("www.google.com is first link, www.facebook.com is second link, www.instagram.com is third link");
textView.setMovementMethod(LinkMovementMethod.getInstance());

最佳答案

在 WebView 所在的 Activity 标签内的 Android list 中添加:

<activity android:name=".WebViewActivity">
<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <data android:scheme="http"
        android:host="*" />

</intent-filter>
</activity>

我在 TextView 上做了:

<TextView
    android:id="@+id/link"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="http://www.google.com"
    android:autoLink="web"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

这对我有用。

关于java - Android:在自定义 Web View 中检测并打开外部链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49408519/

相关文章:

android - 尝试在空对象引用上调用虚拟方法 'java.lang.String android.content.Context.getPackageName()'

android - fragment 还没有附加?

android - 不推荐使用 setTargetFragment() 后如何替换它

java - CDI 注入(inject)抽象类

Android 动态添加联系人表单

java - 如何检测对 JTable 行边界的点击?

android - 在自定义 View 中管理焦点

android - 换行符的歧义行为

java - 警告 : unmappable character for encoding ASCII

java - spring 4.2 应用程序事件使用 Spring MVC 触发两次,为什么?