android WebView 使用实例

Wesley13
• 阅读 598

主布局文件:

<?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" >
    <LinearLayout android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/bt_back"
            android:text="后退"/>
        <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/bt_forward"
            android:text="前进"/>
        <EditText android:layout_width="150px"
            android:layout_height="wrap_content"
            android:id="@+id/ed_url"
            android:singleLine="true"
            android:hint="请输入网址"/>
        <Button android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/bt_go"
            android:text="GO"/>
    </LinearLayout>
    <WebView android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/webview"/>
</LinearLayout>

主活动类WebViewMainActivity.java:

package com.example.ch10;

import com.example.baseexample.R;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.webkit.URLUtil;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class WebViewMainActivity extends Activity {
    private WebView webview ;
    private Button bt_back,bt_forward,bt_go;
    private EditText ed_url;
    
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ch10webviewmain);
        
        webview = (WebView)findViewById(R.id.webview);
        ed_url = (EditText)findViewById(R.id.ed_url);
        bt_back = (Button)findViewById(R.id.bt_back);
        bt_forward = (Button)findViewById(R.id.bt_forward);
        bt_go = (Button)findViewById(R.id.bt_go);
        
        WebSettings webSettings = webview.getSettings();
        webSettings.setBuiltInZoomControls(true);
        webSettings.setDefaultFontSize(9);
        webSettings.setAllowFileAccess(true);
        webview.setWebViewClient(new WebViewClient(){
            public boolean shouldOverrideUrlLoading(WebView view,String url){
                view.loadUrl(url);
                return true;
            }
        });
        
        webview.setWebChromeClient(new WebChromeClient(){
            public void onReceivedTitle(WebView view,String title){
                WebViewMainActivity.this.setTitle(title);
                super.onReceivedTitle(view, title);
            }
            public void onProgressChanged(WebView view,int newProgress){
                WebViewMainActivity.this.getWindow().setFeatureInt(Window.FEATURE_PROGRESS, newProgress*100);
                super.onProgressChanged(view, newProgress);
            }
        });
        
        bt_go.setOnClickListener(new Button.OnClickListener(){

            @Override
            public void onClick(View v) {
                String url = ed_url.getText().toString().trim();
                if(URLUtil.isNetworkUrl(url)){
                    webview.loadUrl(url);
                }else{
                    Toast.makeText(WebViewMainActivity.this, "网址错误", Toast.LENGTH_LONG).show();
                }
            }
            
        });
        
        bt_back.setOnClickListener(new Button.OnClickListener(){

            @Override
            public void onClick(View arg0) {
                if(webview.canGoBack()){
                    webview.goBack();
                }
            }
            
        });
        
        bt_forward.setOnClickListener(new Button.OnClickListener(){

            @Override
            public void onClick(View arg0) {
                
                if(webview.canGoForward()){
                    webview.goForward();
                }
            }
            
        });
        
    }
    
    public boolean onKeyDown(int keyCode,KeyEvent event){
        if((keyCode==KeyEvent.KEYCODE_BACK) && webview.canGoBack()){
            webview.goBack();
            return true;
        }else if(keyCode==KeyEvent.KEYCODE_BACK){
            return super.onKeyDown(keyCode, event);
        }
        return false;
    }

}
点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Wesley13 Wesley13
2年前
android ContextMenu 上下文菜单示例
ch2\_contextmenu.xml:<?xmlversion"1.0"encoding"utf8"?<LinearLayoutxmlns:android"http://schemas.android.com/apk/res/android"android:layout_width"match_p
东方客主 东方客主
3年前
Android输入法遮挡了输入框,使用android:fitsSystemWindows="true"后界面顶部出现白条
问题1、页面布局文件:<LinearLayoutxmlns:android"http://schemas.android.com/apk/res/android"android:id"@id/layoutorderdetail"android:layoutwidth"matchparent"android:layoutheigh
Wesley13 Wesley13
2年前
android Notification 状态栏通知使用示例
ch7\_notification.xml:<?xmlversion"1.0"encoding"utf8"?<LinearLayoutxmlns:android"http://schemas.android.com/apk/res/android"android:layout_width"match_
Stella981 Stella981
2年前
Android toolbar 标题精确居中 不会因返回键偏移
1\.总的布局文件<android.support.design.widget.CoordinatorLayoutxmlns:android"http://schemas.android.com/apk/res/android"xmlns:app"http://schemas.android.com
Stella981 Stella981
2年前
Android 复选框 以及回显
activity\_main.xml<?xmlversion"1.0"encoding"utf8"?<LinearLayoutxmlns:android"http://schemas.android.com/apk/res/android"xmlns:app"http:/
Stella981 Stella981
2年前
Android控件ListView简易使用(使用ArrayAdapter)
<?xmlversion"1.0"encoding"utf8"?<TextViewxmlns:android"http://schemas.android.com/apk/res/android"android:id"@id/tv"android:la
Stella981 Stella981
2年前
LISTVIew 加分割线
分割线样式文件:<?xmlversion"1.0"encoding"utf8"?<layerlistxmlns:android"http://schemas.android.com/apk/res/android"<item<
Stella981 Stella981
2年前
Android 控件抖动效果
利用Android自带的动画效果,实现控件的抖动效果,效果资源文件shark.xml<?xmlversion"1.0"encoding"utf8"?<translatexmlns:android"http://schemas.android.com/apk/res/android"android:fr
Stella981 Stella981
2年前
Android选项卡TabHost功能和用法
1、布局文件<TabHostxmlns:android"http://schemas.android.com/apk/res/android"xmlns:tools"http://schemas.android.com/tools"android:id"@android:id/tabhost"