[英]Links are loading in external app (Browser) issue
I'm showing this website's "https://www.egkhindi.com/" homepage on my app by using this code我正在使用此代码在我的应用程序上显示此网站的“https://www.egkhindi.com/”主页
MainActivity.java MainActivity.java
package com.example.appthree;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String Htmlurl = "https://www.egkhindi.com/";
WebView view = (WebView) this.findViewById(R.id.webView);
view.getSettings().setJavaScriptEnabled(true);
view.loadUrl(Htmlurl);
}
}
This is coming fine but when I'm clicking any link on app , an external browser comes up to load that link.这很好,但是当我单击 app 上的任何链接时,会出现一个外部浏览器来加载该链接。 Is there any fix to load every external link in my app ?
是否有任何修复程序可以加载我的应用程序中的每个外部链接?
for this, you have to create webViewController Class to load view inside app webview为此,您必须创建 webViewController 类以在应用程序 webview 中加载视图
Make changes in your code as below如下更改您的代码
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String Htmlurl = "https://www.egkhindi.com/";
WebView view = (WebView) this.findViewById(R.id.webView);
view.getSettings().setJavaScriptEnabled(true);
view.setWebViewClient(new webViewController()); // added webViewController here
view.loadUrl(Htmlurl);
}
// Webview Controller to handle view inside app webview
private static class webViewController extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url); // to loading view inside webview and return true
return true;
}
}
}
I hope it helps.我希望它有所帮助。
use this settings for your webView :为您的 webView 使用此设置:
kotlin:科特林:
view.webViewClient = object : WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
view.loadUrl(url)
return true
}
}
java:爪哇:
view.setWebViewClient(new mWebViewClient());
private static class mWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url); // to loading view inside webview and return true
return true;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.