- package com.iame.mybrowser;
-
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.content.DialogInterface;
- import android.graphics.Bitmap;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.KeyEvent;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.Window;
- import android.view.View.OnClickListener;
- import android.webkit.JsPromptResult;
- import android.webkit.JsResult;
- import android.webkit.URLUtil;
- import android.webkit.WebChromeClient;
- import android.webkit.WebView;
- import android.webkit.WebViewClient;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
-
-
- public class MyBrowser extends Activity {
- private static final String DEBUG_TAG ="MyBrowser" ;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- //用title bar做进度条
- getWindow().requestFeature(Window.FEATURE_PROGRESS);
- setContentView(R.layout.main);
- //获取WebView
- final WebView webView=(WebView)findViewById(R.id.browser);
- //获取URL,也就是http://www.baidu.com这方面的地址
- final EditText text=(EditText)findViewById(R.id.url);
- //确定按钮
- Button go=(Button)findViewById(R.id.go);
- //设置支持javaScript
- webView.getSettings().setJavaScriptEnabled(true);
-
-
- //WebViewClient负责帮助WebView处理各种通知、请求事件等。想相当于程序嵌套了一个浏览器.浏览器里面的按钮支持事件,就需要WebView。
- webView.setWebViewClient(new WebViewClient(){
- //页面加载完成时要做的事件
- @Override
- public void onPageFinished(WebView view, String url) {
-
-
- super.onPageFinished(view, url);
- }
- //页面加载时要做的事件
- @Override
- public void onPageStarted(WebView view, String url, Bitmap favicon) {
- text.setText(url);
-
- super.onPageStarted(view, url, favicon);
- }
- });
-
- //WebChromeClient负责帮助WebView处理Javascript的对话框,网站图标,网站title,加载进度等。
- webView.setWebChromeClient(new WebChromeClient(){
-
- //1 .弹出的对话框
- @Override
- public boolean onJsAlert(WebView view, String url, String message,final JsResult result) {
-
- AlertDialog.Builder b2 = new AlertDialog.Builder(MyBrowser.this);
- b2.setTitle(R.string.tite1);
- b2.setMessage(message);
- b2.setPositiveButton(R.string.ok,new AlertDialog.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- //按确定后腰执行的代码,这句很重要
- result.confirm();
- //MyBrowser.this.finish();
-
- }
- });
-
- //设置对话框是否取消,默认是可以取消的。
- //这种方式按还回键,齐作用
- //b.setCancelable(true);
- //这种方式按还回键,不齐作用
- b2.setCancelable(false);
- //创建对话框,才能显示
- b2.create();
- b2.show();
- //如果还回的是flase将会多弹出窗口,不是我们想要的结果
- return true;
- };
- //2 .对话框
- @Override
- public boolean onJsConfirm(WebView view, String url, String message,
- final JsResult result) {
- AlertDialog.Builder b3 = new AlertDialog.Builder(MyBrowser.this);
- b3.setTitle(R.string.tite2);
- b3.setMessage(message);
- b3.setPositiveButton(R.string.ok,
- new AlertDialog.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
-
- result.confirm();
-
- }
- });
- b3.setNegativeButton(R.string.no,
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- //按取消做的事件
-
- result.cancel();
-
- }
- });
- b3.setCancelable(false);
- b3.create();
- b3.show();
- return true;
- };
-
- //3 .输入对话框
- @Override
- public boolean onJsPrompt(WebView view, String url, String message,
- String defaultValue, final JsPromptResult result) {
- //获得环境参数
- final LayoutInflater factory = LayoutInflater.from(MyBrowser.this);
- //导入预先设置的输入设置
- final View v = factory.inflate(R.layout.javascript_prompt_dialog, null);
- //获得输入框标题并设置输入框标题
- ((TextView) v.findViewById(R.id.prompt_message_text)).setText(message);
- //获得输入框平设置默认值
- ((EditText) v.findViewById(R.id.prompt_input_field)).setText(defaultValue);
-
- AlertDialog.Builder b4 = new AlertDialog.Builder(MyBrowser.this);
- b4.setTitle(R.string.tite3);
- b4.setView(v);
- b4.setPositiveButton(R.string.ok,
- new AlertDialog.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- String value = ((EditText) v.findViewById(R.id.prompt_input_field)).getText().toString();
- result.confirm(value);
- }
- });
- b4.setNegativeButton(R.string.no,
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- result.cancel();
- }
- });
- b4.setOnCancelListener(new DialogInterface.OnCancelListener() {
- public void onCancel(DialogInterface dialog) {
- result.cancel();
- }
- });
-
- b4.show();
- return true;
- };
-
- //设置网页加载的进度条
- @Override
- public void onProgressChanged(WebView view, int newProgress) {
- MyBrowser.this.getWindow().setFeatureInt(Window.FEATURE_PROGRESS, newProgress * 100 );
-
- super.onProgressChanged(view, newProgress);
- }
- //设置应用程序的标题title
- @Override
- public void onReceivedTitle(WebView view, String title) {
- MyBrowser.this.setTitle(title);
- super.onReceivedTitle(view, title);
- }
- });
-
-
-
- //确定搜索按钮
-
- go.setOnClickListener(new OnClickListener(){
- public void onClick(View v)
- {
- try
- {
- //取得编辑框中我们输入的内容
- String url = text.getText().toString();
- //判断输入的内容是不是网址
- if ( URLUtil.isNetworkUrl(url) )
- {
- //装载网址
- webView.loadUrl(url);
- }
- else
- {
- text.setText("输入网址错误,请重新输入" );
- }
- }
- catch (Exception e)
- {
- Log.e(DEBUG_TAG, e.toString());
- }
- }
- });
-
- webView.loadUrl("file:///android_asset/index.html" );
- }
-
-
- //返回上一页
- public boolean onKeyDown(int keyCode, KeyEvent event) {
- WebView mWebView=(WebView)findViewById(R.id.browser);
- if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
- mWebView.goBack();
- return true;
- }
- return super.onKeyDown(keyCode, event);
- }
- }
package com.iame.mybrowser;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.URLUtil;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MyBrowser extends Activity {
private static final String DEBUG_TAG ="MyBrowser";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//用title bar做进度条
getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.main);
//获取WebView
final WebView webView=(WebView)findViewById(R.id.browser);
//获取URL,也就是http://www.baidu.com这方面的地址
final EditText text=(EditText)findViewById(R.id.url);
//确定按钮
Button go=(Button)findViewById(R.id.go);
//设置支持javaScript
webView.getSettings().setJavaScriptEnabled(true);
//WebViewClient负责帮助WebView处理各种通知、请求事件等。想相当于程序嵌套了一个浏览器.浏览器里面的按钮支持事件,就需要WebView。
webView.setWebViewClient(new WebViewClient(){
//页面加载完成时要做的事件
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
//页面加载时要做的事件
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
text.setText(url);
super.onPageStarted(view, url, favicon);
}
});
//WebChromeClient负责帮助WebView处理Javascript的对话框,网站图标,网站title,加载进度等。
webView.setWebChromeClient(new WebChromeClient(){
//1.弹出的对话框
@Override
public boolean onJsAlert(WebView view, String url, String message,final JsResult result) {
AlertDialog.Builder b2 = new AlertDialog.Builder(MyBrowser.this);
b2.setTitle(R.string.tite1);
b2.setMessage(message);
b2.setPositiveButton(R.string.ok,new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//按确定后腰执行的代码,这句很重要
result.confirm();
//MyBrowser.this.finish();
}
});
//设置对话框是否取消,默认是可以取消的。
//这种方式按还回键,齐作用
//b.setCancelable(true);
//这种方式按还回键,不齐作用
b2.setCancelable(false);
//创建对话框,才能显示
b2.create();
b2.show();
//如果还回的是flase将会多弹出窗口,不是我们想要的结果
return true;
};
//2.对话框
@Override
public boolean onJsConfirm(WebView view, String url, String message,
final JsResult result) {
AlertDialog.Builder b3 = new AlertDialog.Builder(MyBrowser.this);
b3.setTitle(R.string.tite2);
b3.setMessage(message);
b3.setPositiveButton(R.string.ok,
new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
result.confirm();
}
});
b3.setNegativeButton(R.string.no,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//按取消做的事件
result.cancel();
}
});
b3.setCancelable(false);
b3.create();
b3.show();
return true;
};
//3.输入对话框
@Override
public boolean onJsPrompt(WebView view, String url, String message,
String defaultValue, final JsPromptResult result) {
//获得环境参数
final LayoutInflater factory = LayoutInflater.from(MyBrowser.this);
//导入预先设置的输入设置
final View v = factory.inflate(R.layout.javascript_prompt_dialog, null);
//获得输入框标题并设置输入框标题
((TextView) v.findViewById(R.id.prompt_message_text)).setText(message);
//获得输入框平设置默认值
((EditText) v.findViewById(R.id.prompt_input_field)).setText(defaultValue);
AlertDialog.Builder b4 = new AlertDialog.Builder(MyBrowser.this);
b4.setTitle(R.string.tite3);
b4.setView(v);
b4.setPositiveButton(R.string.ok,
new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String value = ((EditText) v.findViewById(R.id.prompt_input_field)).getText().toString();
result.confirm(value);
}
});
b4.setNegativeButton(R.string.no,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
result.cancel();
}
});
b4.setOnCancelListener(new DialogInterface.OnCancelListener() {
public void onCancel(DialogInterface dialog) {
result.cancel();
}
});
b4.show();
return true;
};
//设置网页加载的进度条
@Override
public void onProgressChanged(WebView view, int newProgress) {
MyBrowser.this.getWindow().setFeatureInt(Window.FEATURE_PROGRESS, newProgress * 100);
super.onProgressChanged(view, newProgress);
}
//设置应用程序的标题title
@Override
public void onReceivedTitle(WebView view, String title) {
MyBrowser.this.setTitle(title);
super.onReceivedTitle(view, title);
}
});
//确定搜索按钮
go.setOnClickListener(new OnClickListener(){
public void onClick(View v)
{
try
{
//取得编辑框中我们输入的内容
String url = text.getText().toString();
//判断输入的内容是不是网址
if ( URLUtil.isNetworkUrl(url) )
{
//装载网址
webView.loadUrl(url);
}
else
{
text.setText("输入网址错误,请重新输入");
}
}
catch (Exception e)
{
Log.e(DEBUG_TAG, e.toString());
}
}
});
webView.loadUrl("file:///android_asset/index.html");
}
//返回上一页
public boolean onKeyDown(int keyCode, KeyEvent event) {
WebView mWebView=(WebView)findViewById(R.id.browser);
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
intdex.html
<html xmlns="http://www.w3.org/1999/xhtml">
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
function onAlert(){
alert("This is a alert sample");
}
function onCamera(){
//var personaldata = window.PersonalData.getPersonalData();
var b=confirm("are you sure go to Camera?");
//alert("your choice is "+b);
}
function onPrompt33(){
var b1=prompt("please input your password","yangguangfu");
alert("your input is "+b1);
}
</script>
<pre>
<input type="button" value="Yang_Alert" onclick="onAlert()"/>
<input type="button" id="Personaldata" value="打开相机" onclick="onCamera()"/>
<input type="button" value="Yang_prompt" onclick="onPrompt33()"/>
<a href="http://www.google.com" />Google</a>
</pre>
</html>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.iame.mybrowser" android:versionCode="1"
android:versionName="1.0.0">
<uses-permission android:name="android.permission.INTERNET" />
<application android:icon="@drawable/icon" android:label="MyBroswer">
<activity android:label="MyBrowser" android:name="MyBrowser">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="3" /><!-- 版本标识 -->
<uses-permission android:name="android.permission.INTERNET"/><!-- 访问网络权限 -->
<!-- 在SDCard中创建与删除文件权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<!-- 往SDCard写入数据权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<!-- 调用相机权限 -->
<uses-permission android:name="android.permission.CAMERA"/>
</manifest>