android 应用程序正在打开浏览器应用程序而不是 webView,我该如何阻止它?
现在,当我按下特定按钮时,它会启动将网页打开到 webView 中的意图,但它会打开一个浏览器应用程序,显示地址栏,这不是我想要的。
经过一些研究后,我发现我可以使用 shouldOverrideUrlLoading() 方法来阻止这种情况发生,只需在 webView 中打开页面,但我不知道如何在我的代码中实现此方法,将不胜感激!
这是我在课堂上打开网页的内容:
public class Codes extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.codes);
View titleView = getWindow().findViewById(android.R.id.title);
if (titleView != null) {
ViewParent parent = titleView.getParent();
if (parent != null && (parent instanceof View)) {
View parentView = (View)parent;
parentView.setBackgroundColor(Color.rgb(228, 108, 10));
}
}
WebView codes = (WebView)findViewById(R.id.codesWebView);
codes.getSettings().setJavaScriptEnabled(true);
codes.loadUrl("http://dev10.affirmaconsulting.com:24009/keyentry");
shouldOverrideUrlLoading(codes, "http://dev10.affirmaconsulting.com:24009/keyentry");
}
}
Right now when I push a specific button it starts an intent to open a web page into a webView, but instead it opens a Browser Application, revealing the address bar which is not what I want.
After doing some research I found out I can use the shouldOverrideUrlLoading() method to stop this from happening and just open the page in a webView, but I don't know how to implement this method in my code, help would be greatly appreciated!
This is what I have in the class where I'm opening the web page:
public class Codes extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.codes);
View titleView = getWindow().findViewById(android.R.id.title);
if (titleView != null) {
ViewParent parent = titleView.getParent();
if (parent != null && (parent instanceof View)) {
View parentView = (View)parent;
parentView.setBackgroundColor(Color.rgb(228, 108, 10));
}
}
WebView codes = (WebView)findViewById(R.id.codesWebView);
codes.getSettings().setJavaScriptEnabled(true);
codes.loadUrl("http://dev10.affirmaconsulting.com:24009/keyentry");
shouldOverrideUrlLoading(codes, "http://dev10.affirmaconsulting.com:24009/keyentry");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我遇到了同样的问题,页面没有在我的网络视图中加载,而是通过浏览器应用程序加载。我通过实现 WebViewClient 类并重写 shouldOverrideUrlLoading 方法解决了该问题。
事实证明以下链接很有帮助:
http://www.firstdroid。 com/2010/08/05/override-url-loading-in-webview-android-tutorial/
I had this same problem, that pages were not loading in my webview but instead via the browser application. I resolved the issue by implementing a WebViewClient class and overriding the shouldOverrideUrlLoading method.
The following link proved to be helpful:
http://www.firstdroid.com/2010/08/05/override-url-loading-in-webview-android-tutorial/
我们在一项活动中执行了这一具体任务,但尚未遇到此问题。这是我们的样子:
在布局文件中定义的 webview 看起来像这样:
希望这会有所帮助。
We do this exact task in one of our activities and haven't ran into this issue. Here is what ours looks like:
With the webview defined in the layout file looking like this:
Hope this helps.