有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Android:在自定义web视图中检测并打开外部链接

我正面临一个奇怪的问题,尽管我付出了所有的努力,但仍然无法解决它。 问题是: 我有一个textview,它直接从Twitter加载数据。Tweets可能包含许多链接,并显示在textview中,但单击链接后,它们将在安卓 default browser中打开。我想做的是当用户单击任何链接how will I detect which link has been clickedopen my webview instead of default browser

以下是我迄今为止取得的成果:

    <TextView
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:text="Hello www.google.com Thank You"
    安卓:autoLink="web"
    安卓: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" />

爪哇

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

共 (1) 个答案

  1. # 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" />
    

    这对我有用