主布局文件:

<?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(){@Overridepublic 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(){@Overridepublic void onClick(View arg0) {if(webview.canGoBack()){webview.goBack();}}});bt_forward.setOnClickListener(new Button.OnClickListener(){@Overridepublic 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;}}






更多相关文章

  1. 定义界面布局TextView&Button
  2. Android(安卓)Studio用线性布局和相对布局做一个简单的登陆界面
  3. android wearOs开发
  4. Android布局 屏幕滚动方法 ScrollView
  5. android--widget之menu
  6. android.support.v7.app.AlertDialog显示异常
  7. AdapterTest
  8. radioButton 使用
  9. ListActivity简介

随机推荐

  1. android 动画学习系列(一)
  2. android包Android "java.lang.NoClassDef
  3. ACTION_PICK与ACTION_GET_CONTENT
  4. Handler详解
  5. Android中 int 和 String 互相转换的多种
  6. android 动态添加组件(RadioGroup 添加Rad
  7. Android事件机制(三)
  8. Unity5.0与Android交互
  9. android donut 飞行模式分析
  10. Android(安卓)ViewFlipper触摸动画