android 应用程序正在打开浏览器应用程序而不是 webView,我该如何阻止它?

发布于 2024-10-21 21:23:32 字数 1069 浏览 4 评论 0原文

现在,当我按下特定按钮时,它会启动将网页打开到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

成熟稳重的好男人 2024-10-28 21:23:32

我遇到了同样的问题,页面没有在我的网络视图中加载,而是通过浏览器应用程序加载。我通过实现 WebViewClient 类并重写 shouldOverrideUrlLoading 方法解决了该问题。

    WebViewClient webClient = new WebViewClient()
    {
        @Override
        public boolean shouldOverrideUrlLoading(WebView  view, String  url)
        {
            return false;
        }
    };
    webView.setWebViewClient(webClient);

事实证明以下链接很有帮助:
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.

    WebViewClient webClient = new WebViewClient()
    {
        @Override
        public boolean shouldOverrideUrlLoading(WebView  view, String  url)
        {
            return false;
        }
    };
    webView.setWebViewClient(webClient);

The following link proved to be helpful:
http://www.firstdroid.com/2010/08/05/override-url-loading-in-webview-android-tutorial/

旧话新听 2024-10-28 21:23:32

我们在一项活动中执行了这一具体任务,但尚未遇到此问题。这是我们的样子:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.document_viewer_activity);

    WebView wv = (WebView) findViewById(R.id.documentViewerWebview);
    wv.getSettings().setJavaScriptEnabled(true);

    String url = getString(R.string.document_viewer_url) + 
            getIntent().getExtras().getString("media");
    wv.loadUrl(url);

}

在布局文件中定义的 webview 看起来像这样:

  <WebView
        android:id="@+id/documentViewerWebview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
 />

希望这会有所帮助。

We do this exact task in one of our activities and haven't ran into this issue. Here is what ours looks like:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.document_viewer_activity);

    WebView wv = (WebView) findViewById(R.id.documentViewerWebview);
    wv.getSettings().setJavaScriptEnabled(true);

    String url = getString(R.string.document_viewer_url) + 
            getIntent().getExtras().getString("media");
    wv.loadUrl(url);

}

With the webview defined in the layout file looking like this:

  <WebView
        android:id="@+id/documentViewerWebview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
 />

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文