最新消息: USBMI致力于为网友们分享Windows、安卓、IOS等主流手机系统相关的资讯以及评测、同时提供相关教程、应用、软件下载等服务。

WebView浏览网页,在APP内打开不调用系统浏览器

业界 admin 1浏览 0评论
package com.example.test;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

public class MainActivity1 extends Activity{
	EditText urlEditText;
	WebView showView;
	Button button;
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main); 
		urlEditText = (EditText)findViewById(R.id.url);
		showView = (WebView)findViewById(R.id.show);
        WebSettings settings = showView.getSettings();  
        //适应屏幕  
        settings.setUseWideViewPort(true);  
        settings.setSupportZoom(true);  
//      settings.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);  
//      settings.setLoadWithOverviewMode(true);  
//      settings.setBuiltInZoomControls(true);  
        settings.setJavaScriptEnabled(true);  
        showView.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url)
                                   { //  重写此方法表明点击网页里面的链接还是在当前的webview里跳转,不跳到浏览器那边
                                           view.loadUrl(url);
                                           return true;
                                   }
            });
		button = (Button)findViewById(R.id.button);
		button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String urlStr = urlEditText.getText().toString();
				if(urlStr.startsWith("http://www.")){
					showView.loadUrl(urlStr);
				} else if(urlStr.startsWith("www.")){
					showView.loadUrl("http://"+urlStr);
				} else {
					showView.loadUrl("http://www."+urlStr);
				}
			}
		});
	}
}

package com.example.test;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

public class MainActivity1 extends Activity{
	EditText urlEditText;
	WebView showView;
	Button button;
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main); 
		urlEditText = (EditText)findViewById(R.id.url);
		showView = (WebView)findViewById(R.id.show);
        WebSettings settings = showView.getSettings();  
        //适应屏幕  
        settings.setUseWideViewPort(true);  
        settings.setSupportZoom(true);  
//      settings.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);  
//      settings.setLoadWithOverviewMode(true);  
//      settings.setBuiltInZoomControls(true);  
        settings.setJavaScriptEnabled(true);  
        showView.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url)
                                   { //  重写此方法表明点击网页里面的链接还是在当前的webview里跳转,不跳到浏览器那边
                                           view.loadUrl(url);
                                           return true;
                                   }
            });
		button = (Button)findViewById(R.id.button);
		button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String urlStr = urlEditText.getText().toString();
				if(urlStr.startsWith("http://www.")){
					showView.loadUrl(urlStr);
				} else if(urlStr.startsWith("www.")){
					showView.loadUrl("http://"+urlStr);
				} else {
					showView.loadUrl("http://www."+urlStr);
				}
			}
		});
	}
}

发布评论

评论列表 (0)

  1. 暂无评论