package com.iteedu.webview; import android.app.Activity; import android.app.AlertDialog; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnKeyListener; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class WebViewExample extends Activity implements OnClickListener, OnKeyListener { Button zoomin; Button zoomout; Button info_title; Button info_url; EditText url; WebView webView; //Example04程序 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.example04); //建立显示画面上Button,EditText,WebView类别的实例变量 zoomin = (Button)findViewById(R.id.Button01); zoomout = (Button)findViewById(R.id.Button02); info_title = (Button)findViewById(R.id.Button03); info_url = (Button)findViewById(R.id.Button04); url = (EditText)findViewById(R.id.EditText01); webView = (WebView)findViewById(R.id.WebView01); webView.setWebViewClient(new WebViewClient()); //设定Button和EditText的监听功能 zoomin.setOnClickListener(this); zoomout.setOnClickListener(this); info_title.setOnClickListener(this); info_url.setOnClickListener(this); url.setOnKeyListener(this); } //按下Button时的处理程序 public void onClick(View v) { if (v == zoomin) { boolean ret = webView.zoomIn(); Toast.makeText(this, "zoom in is "+ret, Toast.LENGTH_SHORT).show(); } else if (v == zoomout) { boolean ret = webView.zoomOut(); Toast.makeText(this, "zoom Out is "+ret, Toast.LENGTH_SHORT).show(); } else if (v == info_title) { String title = webView.getTitle(); new AlertDialog.Builder(this) .setTitle("Title") .setMessage(title) .setPositiveButton("Ok", null) .show(); } else if (v == info_url) { String url = webView.getUrl(); new AlertDialog.Builder(this) .setTitle("URL") .setMessage(url) .setPositiveButton("Ok", null) .show(); } } //於url(EditText)编辑框按下ENTER时的处理程序,下载載新的网页 public boolean onKey(View v, int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_ENTER) { webView.loadUrl(url.getText().toString()); return true; } return false; } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"> <LinearLayout android:id="@+id/LinearLayout02" android:layout_height="wrap_content" android:layout_width="fill_parent"> <Button android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="放大" /> <Button android:id="@+id/Button02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="缩小" /> <Button android:id="@+id/Button03" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="标题信息"></Button> <Button android:id="@+id/Button04" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="链接信息"></Button> </LinearLayout> <EditText android:id="@+id/EditText01" android:layout_height="wrap_content" android:text="http://" android:layout_width="fill_parent" android:maxLines="1" /> <WebView android:id="@+id/WebView01" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>