他们如何在webview中加载url而不是androids内部浏览器?

他们如何在webview中加载url而不是androids内部浏览器?,android,webview,Android,Webview,我正在努力做到这一点:。 为了得到确切的提示,请看“下载源代码”之前的最后一张图片 我的应用程序代码是: bookingView = (WebView) findViewById(R.id.fullscreen_content); bookingView.getSettings().setJavaScriptEnabled(true); bookingView.loadUrl("http://www.google.com"); 但这段代码的作用是在android的默认浏览器/内部浏览器中打开u

我正在努力做到这一点:。 为了得到确切的提示,请看“下载源代码”之前的最后一张图片

我的应用程序代码是:

bookingView = (WebView) findViewById(R.id.fullscreen_content);
bookingView.getSettings().setJavaScriptEnabled(true);
bookingView.loadUrl("http://www.google.com");
但这段代码的作用是在android的默认浏览器/内部浏览器中打开url(在本例中为Google),而这并不意味着在我正在制作的android应用程序中


有什么想法吗?

loadUrl()调用之前添加这一行

bookingView.setWebViewClient(new WebViewClient());

这就是答案:

功能

  • 在WebView上加载URL
  • 在本地浏览器的Webview上打开网站中的另一页
  • 如果您按下Back按钮,将进入before页面,请勿退出应用程序
  • MainActivity.java

    public class MainActivity extends AppCompatActivity {
    
            WebView webview;
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                webview = (WebView)findViewById(R.id.webview);
                webView();
            }
    
        //Metodo llamar el webview
        private void webView(){
            //Habilitar JavaScript (Videos youtube)
            webview.getSettings().setJavaScriptEnabled(true);
    
            //Handling Page Navigation
            webview.setWebViewClient(new MyWebViewClient());
    
            //Load a URL on WebView
            webview.loadUrl("http://stackoverflow.com/");
        }
    
        // Metodo Navigating web page history
        @Override public void onBackPressed() {
            if(webview.canGoBack()) {
                webview.goBack();
            } else {
                super.onBackPressed();
            }
        }
    
        // Subclase WebViewClient() para Handling Page Navigation
        private class MyWebViewClient extends WebViewClient {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (Uri.parse(url).getHost().equals("stackoverflow.com")) { //Force to open the url in WEBVIEW
                    return false;
                }
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(intent);
                return true;
            }
        }
    
    }
    
    活动\u main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <WebView android:id="@+id/webview"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
    </LinearLayout>
    
    <uses-permission android:name="android.permission.INTERNET" />
    
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.webview" >
    
        <uses-permission android:name="android.permission.INTERNET" />
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name=".MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    
    </manifest>
    
    
    
    将其包含在AndroidManifest.xml中

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <WebView android:id="@+id/webview"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
    </LinearLayout>
    
    <uses-permission android:name="android.permission.INTERNET" />
    
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.webview" >
    
        <uses-permission android:name="android.permission.INTERNET" />
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name=".MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    
    </manifest>
    
    
    
    AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <WebView android:id="@+id/webview"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
    </LinearLayout>
    
    <uses-permission android:name="android.permission.INTERNET" />
    
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.webview" >
    
        <uses-permission android:name="android.permission.INTERNET" />
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name=".MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    
    </manifest>
    
    
    

    网站的另一个网页在WEBVIEW上