Android Webview - 打开我的浏览器

时间:2014-10-31 11:23:51

标签: java android webview

我创建了一个Android webview,但在我的手机中加载后打开浏览器,我不想

我的手机是一个三星Galaxy ace plus(android版本2.3.6) 应用程序创建到Android版+2.3.3

AndroidManifest.xml有:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="ivoclar.pack"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/android" android:label="@string/app_name">
        <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>
    <uses-permission android:name="android.permission.INTERNET" />
</manifest> 

MainActivity.java有:

package ivoclar.pack;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;

public class MainActivity extends Activity
{
    /** Called when the activity is first created.
     * @param savedInstanceState */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        WebView myWebView;
        myWebView = (WebView) findViewById(R.id.webview);
        myWebView.loadUrl("http://gastro.hero.ch");

    }
}

layout - main.xml有:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

    <WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    />

</LinearLayout>

4 个答案:

答案 0 :(得分:1)

试试这种方式

    String new_url ="http://gastro.hero.ch";
    myWebView = (WebView) findViewById(R.id.webview);

    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setDomStorageEnabled(true);
    webView.setWebChromeClient(new WebChromeClient());

    webView.loadUrl(new_url);

    webView.setWebViewClient(new WebViewClient() {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

                // view.loadUrl(url);
                System.out.println("url called:::" + url);
                if (url.startsWith("tel:")) {
                    Intent intent = new Intent(Intent.ACTION_DIAL, Uri
                            .parse(url));
                    startActivity(intent);
                } else if (url.startsWith("http:")
                        || url.startsWith("https:")) {

                    return false;
                } else if (url.startsWith("mailto:")) {

                    String row[] = url.split("mailto:");
                    String email_add = row[1];

                } else {

                    view.loadUrl(url);
                    return true;
                }

            return true;
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            // TODO Auto-generated method stub
            super.onPageStarted(view, url, favicon);

        }

        @Override
        public void onPageFinished(WebView view, String url) {
            // TODO Auto-generated method stub

        }

    });
}

输出:

enter image description here

答案 1 :(得分:0)

只需添加

    myWebView.getSettings().setJavaScriptEnabled(true);
    myWebView.setWebViewClient(new MyWebViewClient());

private class MyWebViewClient extends WebViewClient {
      @Override
      public boolean shouldOverrideUrlLoading(WebView view, String url) {
         view.loadUrl(url);
         return true;
      }
   }

onCreate()

    myWebView.setWebViewClient(new WebViewClient(){
          @Override
          public boolean shouldOverrideUrlLoading(WebView view, String url) {
             view.loadUrl(url);
             return true;
          }
});

答案 2 :(得分:0)

启用javascript:

myWebView.getSettings().setJavaScriptEnabled(true);

覆盖网址重定向:

myWebView.setWebViewClient(new WebViewClient());

答案 3 :(得分:0)

更正!!!

我的更正代码:

<强>的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="ivoclar.pack"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/android" android:label="@string/app_name">
        <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>
    <uses-permission android:name="android.permission.INTERNET" />
</manifest> 

<强> MainActivity.java

package ivoclar.pack;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;


public class MainActivity extends Activity
{

    String url = "http://gastro.hero.ch/index.php";

    /** Called when the activity is first created.
     * @param savedInstanceState */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        WebView myWebView;
        myWebView = (WebView) findViewById(R.id.webview);
        myWebView.getSettings().setJavaScriptEnabled(true);
        myWebView.setWebChromeClient(new WebChromeClient());

        myWebView.loadUrl(url);


        myWebView.setWebViewClient
        (new WebViewClient(){

            public boolean shouldOverrideUrlLoading(WebView view, String url){
                view.loadUrl(url);
                return false;
            }

        });

        //myWebView.loadUrl("http://gastro.hero.ch/");

    }
}

布局 - main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

    <WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    />

</LinearLayout>

泰:

Pedro Oliveira

Jibran Khan

M D