我遇到了一个奇怪的问题,我费了九牛二虎之力还是解决不了。
问题是:
我有一个直接从 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/