解决中文乱码问题:
1.使用getBytes(“编码方式”);;来对汉字进行重编码,得到他的字节数组。
2. 再使用new String(Bytes[],”解码方式”);对字节数组进行相应的解码。
webKit应用
webKit分三个模块:javascriptCore,webCore和webKit.
WebView浏览网页:
几个重要的地方:
WebViewClient是专门辅助WebView处理各种通知,请求等事件的类。
webChromeClient是辅助WebView处理javascript的对话框,网站图标,网站title,加载进度。
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.os.Bundle;
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.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.king.android.R;
/**
* 描述:浏览网页
* 作者:Andy.Liu
* 时间: 2012-7-26 上午07:28:17
**/
public class WebViewActivity extends Activity {
Context mContext;
Button btnConn;
EditText etUrl;
WebView wvShow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = WebViewActivity.this;
setContentView(R.layout.webview_layout);
btnConn = (Button) findViewById(R.id.btn_conn);
etUrl = (EditText) findViewById(R.id.ed_show);
wvShow = (WebView) findViewById(R.id.wv_view);
btnConn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
try{
String url = etUrl.getText().toString();
if(URLUtil.isNetworkUrl(url)){
wvShow.loadUrl(url);
}else{
etUrl.setText("输入网址错误,请重新输入");
}}catch(Exception e){
}
}});
下面是主要知识点:
//设置javascript脚本
WebSettings webSettings = wvShow.getSettings();
webSettings.setJavaScriptEnabled(true);
//设置可以访问文件
webSettings.setAllowFileAccess(true);
//设置支持缩放
webSettings.setBuiltInZoomControls(true);
/**
* 设置WebViewClient
* WebViewClient是专门辅助WebView处理各种通知,请求等事件的类。
* */
wvShow.setWebViewClient(webViewClient);
/**
* webChromeClient是辅助WebView处理javascript的对话框,网站图标,网站title,加载进度。
* */
wvShow.setWebChromeClient(webChromeClient);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(KeyEvent.KEYCODE_BACK==keyCode){
//返回前一个页面
wvShow.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
private WebViewClient webViewClient = new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view,String url) {
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view,String url){
super.onPageFinished(view, url);
}
public void onPageStarted(WebView view,String url,Bitmap favicon){
super.onPageStarted(view, url,favicon);
}
};
private WebChromeClient webChromeClient = new WebChromeClient(){
//处理js中的alert
public boolean onJsAlert(WebView view, String url, String message,final JsResult result) {
Builder builder = new Builder(mContext);
builder.setTitle("提示对话框");
builder.setMessage(message);
builder.setPositiveButton(android.R.string.ok, new AlertDialog.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
//点击确定按键之后,继续执行网页中的操作
result.confirm();
}});
builder.setCancelable(false);
builder.create();
builder.show();
return true;
};
public boolean onJsConfirm(WebView view, String url, String message, final android.webkit.JsResult result) {
Builder builder = new Builder(mContext);
builder.setTitle("带选择的对话框");
builder.setMessage(message);
builder.setPositiveButton(android.R.string.ok, new AlertDialog.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
//点击确定按键之后,继续执行网页中的操作
result.confirm();
}});
builder.setNegativeButton(android.R.string.cancel, new AlertDialog.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
//点击取消
result.cancel();
}});
builder.setCancelable(false);
builder.create();
builder.show();
return true;
};
@Override
//处理javascript中的prompt
//message为网页中对话框的提示内容
//defaultValue在没有输入时,默认显示的内容
public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, final JsPromptResult result) {
//自定义一个带输入的对话框由TextView和EditText构成
final LayoutInflater factory = LayoutInflater.from(mContext);
final View dialogview = factory.inflate(R.layout.prom_dialog, null);
((TextView) dialogview.findViewById(R.id.TextView_PROM)).setText(message);
((EditText) dialogview.findViewById(R.id.EditText_PROM)).setText(defaultValue);
Builder builder = new Builder(mContext);
builder.setTitle("带输入的对话框");
builder.setView(dialogview);
builder.setPositiveButton(android.R.string.ok,
new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//点击确定之后,取得输入的值,传给网页处理
String value = ((EditText) dialogview.findViewById(R.id.EditText_PROM)).getText().toString();
result.confirm(value);
}
});
builder.setNegativeButton(android.R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
result.cancel();
}
});
builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
public void onCancel(DialogInterface dialog) {
result.cancel();
}
});
builder.show();
return true;
};
@Override
//设置网页加载的进度条
public void onProgressChanged(WebView view, int newProgress)
{
WebViewActivity.this.getWindow().setFeatureInt(Window.FEATURE_PROGRESS, newProgress * 100);
super.onProgressChanged(view, newProgress);
}
@Override
//设置应用程序的标题title
public void onReceivedTitle(WebView view, String title)
{
WebViewActivity.this.setTitle(title);
super.onReceivedTitle(view, title);
}
};
}
需要注意webView的几个地方