[英]Open desired link in webview
I want to open link which is in text view in the form of text:type ="autolink" and it contains some URL but when a user tap on it, opens browser instead of webview activity我想以 text:type ="autolink" 的形式打开文本视图中的链接,它包含一些 URL,但是当用户点击它时,会打开浏览器而不是 webview 活动
For Example例如
{
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:autoLink="web"
android:text="www.facebook.com"
android:id="@+id/iimage" />
}
so please tell if a user clicks on this link it opens in webView in WebView activity.所以请告诉用户是否点击此链接,它会在 WebView 活动中的 webView 中打开。 How to do this
如何做到这一点
You will need to set a Spannable to your textview and set the click listener on that Spannable.您需要为 textview 设置一个 Spannable 并在该 Spannable 上设置单击侦听器。
Here you have an example i took from some answer: Android: ClickableSpan in clickable TextView这里有一个我从一些答案中获取的示例: Android: ClickableSpan in clickable TextView
TextView tv = (TextView)findViewById(R.id.textview01);
Spannable span = Spannable.Factory.getInstance().newSpannable("test link span");
span.setSpan(new ClickableSpan() {
@Override
public void onClick(View v) {
Log.d("main", "link clicked");
Toast.makeText(Main.this, "link clicked", Toast.LENGTH_SHORT).show();
} }, 5, 9, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
// All the rest will have the same spannable.
ClickableSpan cs = new ClickableSpan() {
@Override
public void onClick(View v) {
Log.d("main", "textview clicked");
Toast.makeText(Main.this, "textview clicked", Toast.LENGTH_SHORT).show();
} };
// set the "test " spannable.
span.setSpan(cs, 0, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
// set the " span" spannable
span.setSpan(cs, 6, span.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(span);
tv.setMovementMethod(LinkMovementMethod.getInstance());
Look How can we open TextView's links into Webview看看我们如何将TextView的链接打开到Webview中
But actually all you need is webView.loadUrl("www.facebook.com")但实际上你只需要 webView.loadUrl("www.facebook.com")
You can do this simply by:您可以简单地通过以下方式执行此操作:
iimage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(currentactivity, youractivity.class);
intent.putExtra("url", your_url );
startActivity(intent);
finish();
}
});
Inside onCreate of your youractivity在你的活动的 onCreate 里面
String url = getIntent().getStringExtra("url");
webView.loadUrl(url);
Hope this helps.希望这会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.