【问题标题】:Android: Detect and Open external links in custom web viewAndroid:在自定义 Web 视图中检测并打开外部链接
【发布时间】:2018-08-30 16:50:55
【问题描述】:

我付出了所有的努力仍然无法解决它,但我遇到了一个奇怪的问题。 问题是: 我有一个直接从Twitter 加载数据的文本视图。 Tweets 可能包含许多链接,它会显示在 textview 中,但在单击链接时,它们会在 android default browser 中打开。我想要做的是当用户点击任何链接how will I detect which link has been clickedopen my webview instead of default browser

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

    <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());

【问题讨论】:

标签: java android android-fragments android-activity webview


【解决方案1】:

在 WebView 所在的活动标记内的 Android 清单中添加:

<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" />

这对我有用。

【讨论】:

    猜你喜欢
    • 2019-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-07
    • 1970-01-01
    • 1970-01-01
    • 2023-04-11
    • 1970-01-01
    相关资源
    最近更新 更多