android WebView 显示网页

<?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">
    
    <include layout="@layout/uzone_top_bar" />

    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
     android:layout_height="40dp"
    android:gravity="center_vertical" >

    <TextView
        android:id="@+id/uzone_top_TextView_title"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerVertical="true"
        android:gravity="center"
        android:text=""
        android:textSize="18sp" />

    <RelativeLayout
        android:id="@+id/uzone_top_RelativeLayout_cancel"
        android:layout_width="50dp"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:gravity="center" >

        <ImageView
            android:id="@+id/uzone_top_ImageView_cancel"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_marginLeft="10dp"
            android:paddingLeft="10dp"
            android:paddingTop="10dp" />
    </RelativeLayout>

    <ImageView
        android:id="@+id/uzone_top_ImageView_line"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_marginRight="2dp"
        android:layout_toLeftOf="@+id/uzone_top_Button_ok"
        android:visibility="gone" />

    <Button
        android:id="@+id/uzone_top_Button_ok"
        android:layout_width="70dip"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:layout_gravity="center"
        android:text="按钮"
        android:textColor="@android:color/white"
        android:textSize="18sp"
        android:visibility="gone" />

</RelativeLayout>
public class WebBrowserActivity extends Activity
{
    /**
     * UshequMobile地址
     */
    public static final String URL_PREFIX = "http://10.10.9.51:8080/UshequMobile/";
    
    public static final String USERAGENT = "haiersoft.webbrowser";
    
    private WebView webView;
    private TextView title; 
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.web_browser);
        
        //标题
        title = (TextView) findViewById(R.id.uzone_top_TextView_title);
        
        //返回按钮
        RelativeLayout back = (RelativeLayout) findViewById(R.id.uzone_top_RelativeLayout_cancel);
        back.setOnClickListener(new OnClickListener() 
        {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        
        webView = (WebView) findViewById(R.id.webview);
        configWebView();
        
        Intent intent = getIntent();
        String url = intent.getStringExtra("url");
        if(null != url)
        {
            webView.loadUrl(url);
        }
        else
        {
            Toast.makeText(this, "url为空", Toast.LENGTH_SHORT).show();
        }
    }

    private void configWebView()
    {
        WebSettings webSettings = webView.getSettings();
       webSettings.setSupportZoom(false);//设置不可缩放
        webSettings.setJavaScriptEnabled(true); //设置支持javascript
        webSettings.setUserAgentString(USERAGENT);//设置值用于Web服务判断访问来源
//WebViewClient就是帮助WebView处理各种通知、请求事件的,具体来说包括:  webView.setWebViewClient(
new WebViewClient() { public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); } public void onPageStarted(WebView view, String url, Bitmap favicon) { super.onPageStarted(view, url, favicon); } @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { super.onReceivedError(view, errorCode, description, failingUrl); Toast.makeText(getApplicationContext(), description, Toast.LENGTH_LONG).show(); } });

    //WebChromeClient主要处理解析,渲染网页等浏览器做的事情


    //WebChromeClient是辅助WebView处理Javascript的对话框,网站图标,网站title,加载进度等 


        webView.setWebChromeClient(new WebChromeClient() //游览器
        {
            @Override
            public boolean onJsAlert(WebView view, String url, String message, JsResult result) //对话框
            {
//                result.confirm();
//                return true;
                return super.onJsAlert(view, url, message, result);
            }

            @Override
            public void onReceivedTitle(WebView view, String title)  //标题
            {
                super.onReceivedTitle(view, title);
                
                WebBrowserActivity.this.title.setText(title);
            }
            
        });
        
    
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)  //按键响应
    {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) 
        { 
            webView.goBack(); 
            return true; 
        } 
        
        return super.onKeyDown(keyCode, event); 
    }

    @Override
    protected void onPause()
    {
        super.onPause();
    }

    @Override
    protected void onRestart()
    {
        super.onRestart();
    }

    @Override
    protected void onResume()
    {
        super.onResume();
    }
    

    
    
}
原文地址:https://www.cnblogs.com/songyao/p/4169632.html