为什么 我将一个网上的例子下下来运行不好使呢?我想实现点击按钮放大和缩小webview的页面这个功能。求帮助谢谢。这是我的activitypackage com.hundsun.zhoujl.android;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 TestZoomActivity 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:<?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>
我是在模拟器上运行的 版本android2.2
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 TestZoomActivity 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:<?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>
我是在模拟器上运行的 版本android2.2
解决方案 »
- 怎么让自己的Android程序和自己的网站同步?
- 打算一个月将android应用程序开发看一下,各位大虾给点建议
- 一年android经验,android要怎么提高?????
- fill_parent导致后来的控件无法显示了的问题?
- 长按响应在真机上不能实现
- 关于android 出现“Unfortunately,程序名 has stopped” 的问题
- android 怎么查找一个相同的图块在多张图片中不同的位置
- android调用.net的webservice,琢磨了很长时间没发现问题所在,求助
- 请问UC浏览器的地址栏是怎么实现的啊?
- 如何给已root(无supersu)的安卓app授权?
- android mediaplayer问题
- 怎么同时安装android sdk1.5-4.0 的各版本
就可以了
webview.getSettings().setBuiltInZoomControls(true);
webview.getSettings().setSupportZoom(true);
webview.getSettings().setBuiltInZoomControls(true);
这两句话之外 还需要注意你的测试网址 最好是google 因为有的网页不支持啥原因我也不清楚
还有就是用多点触控实现放大缩小除了上边两句话 还应该注意你的布局问题 要不然的话网页就会产生异常