android WebView 使用实例

主布局文件:

1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 <LinearLayout android:orientation="horizontal" 7 android:layout_width="fill_parent" 8 android:layout_height="wrap_content"> 9 <Button android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:id="@+id/bt_back" 12 android:text="后退"/> 13 <Button android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:id="@+id/bt_forward" 16 android:text="前进"/> 17 <EditText android:layout_width="150px" 18 android:layout_height="wrap_content" 19 android:id="@+id/ed_url" 20 android:singleLine="true" 21 android:hint="请输入网址"/> 22 <Button android:layout_width="fill_parent" 23 android:layout_height="wrap_content" 24 android:id="@+id/bt_go" 25 android:text="GO"/> 26 </LinearLayout> 27 <WebView android:layout_width="fill_parent" 28 android:layout_height="fill_parent" 29 android:id="@+id/webview"/> 30</LinearLayout>

主活动类WebViewMainActivity.java:

1package com.example.ch10; 2 3import com.example.baseexample.R; 4 5import android.app.Activity; 6import android.os.Bundle; 7import android.view.KeyEvent; 8import android.view.View; 9import android.view.Window; 10import android.webkit.URLUtil; 11import android.webkit.WebChromeClient; 12import android.webkit.WebSettings; 13import android.webkit.WebView; 14import android.webkit.WebViewClient; 15import android.widget.Button; 16import android.widget.EditText; 17import android.widget.Toast; 18 19public class WebViewMainActivity extends Activity { 20 private WebView webview ; 21 private Button bt_back,bt_forward,bt_go; 22 private EditText ed_url; 23 24 public void onCreate(Bundle savedInstanceState){ 25 super.onCreate(savedInstanceState); 26 setContentView(R.layout.ch10webviewmain); 27 28 webview = (WebView)findViewById(R.id.webview); 29 ed_url = (EditText)findViewById(R.id.ed_url); 30 bt_back = (Button)findViewById(R.id.bt_back); 31 bt_forward = (Button)findViewById(R.id.bt_forward); 32 bt_go = (Button)findViewById(R.id.bt_go); 33 34 WebSettings webSettings = webview.getSettings(); 35 webSettings.setBuiltInZoomControls(true); 36 webSettings.setDefaultFontSize(9); 37 webSettings.setAllowFileAccess(true); 38 webview.setWebViewClient(new WebViewClient(){ 39 public boolean shouldOverrideUrlLoading(WebView view,String url){ 40 view.loadUrl(url); 41 return true; 42 } 43 }); 44 45 webview.setWebChromeClient(new WebChromeClient(){ 46 public void onReceivedTitle(WebView view,String title){ 47 WebViewMainActivity.this.setTitle(title); 48 super.onReceivedTitle(view, title); 49 } 50 public void onProgressChanged(WebView view,int newProgress){ 51 WebViewMainActivity.this.getWindow().setFeatureInt(Window.FEATURE_PROGRESS, newProgress*100); 52 super.onProgressChanged(view, newProgress); 53 } 54 }); 55 56 bt_go.setOnClickListener(new Button.OnClickListener(){ 57 58 @Override 59 public void onClick(View v) { 60 String url = ed_url.getText().toString().trim(); 61 if(URLUtil.isNetworkUrl(url)){ 62 webview.loadUrl(url); 63 }else{ 64 Toast.makeText(WebViewMainActivity.this, "网址错误", Toast.LENGTH_LONG).show(); 65 } 66 } 67 68 }); 69 70 bt_back.setOnClickListener(new Button.OnClickListener(){ 71 72 @Override 73 public void onClick(View arg0) { 74 if(webview.canGoBack()){ 75 webview.goBack(); 76 } 77 } 78 79 }); 80 81 bt_forward.setOnClickListener(new Button.OnClickListener(){ 82 83 @Override 84 public void onClick(View arg0) { 85 86 if(webview.canGoForward()){ 87 webview.goForward(); 88 } 89 } 90 91 }); 92 93 } 94 95 public boolean onKeyDown(int keyCode,KeyEvent event){ 96 if((keyCode==KeyEvent.KEYCODE_BACK) && webview.canGoBack()){ 97 webview.goBack(); 98 return true; 99 }else if(keyCode==KeyEvent.KEYCODE_BACK){ 100 return super.onKeyDown(keyCode, event); 101 } 102 return false; 103 } 104 105}
点赞
收藏

评论区

加载中...

相关推荐

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

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

android ContextMenu 上下文菜单示例

ch2\_contextmenu.xml:<?xmlversion"1.0"encoding"utf8"?<LinearLayoutxmlns:android"http://schemas.android.com/apk/res/android"android:layout_width"match_p

android Notification 状态栏通知使用示例

ch7\_notification.xml:<?xmlversion"1.0"encoding"utf8"?<LinearLayoutxmlns:android"http://schemas.android.com/apk/res/android"android:layout_width"match_

Android输入法遮挡了输入框,使用android:fitsSystemWindows="true"后界面顶部出现白条

问题1、页面布局文件:<LinearLayoutxmlns:android"http://schemas.android.com/apk/res/android"android:id"@id/layoutorderdetail"android:layoutwidth"matchparent"android:layoutheigh

Android 复选框 以及回显

activity\_main.xml<?xmlversion"1.0"encoding"utf8"?<LinearLayoutxmlns:android"http://schemas.android.com/apk/res/android"xmlns:app"http:/