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.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.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 {  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        getWindow().requestFeature(Window.FEATURE_PROGRESS);//��title bar��������   
        setContentView(R.layout.main);  
        final WebView webView=(WebView)findViewById(R.id.browser);  
        final EditText text=(EditText)findViewById(R.id.url);  
        Button go=(Button)findViewById(R.id.go);  
        webView.getSettings().setJavaScriptEnabled(true);  
        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);  
            }  
        });  
        webView.setWebChromeClient(new WebChromeClient(){  
            @Override  
            public boolean onJsAlert(WebView view, String url, String message,  
                    final JsResult result) {  
                AlertDialog.Builder b = new AlertDialog.Builder(MyBrowser.this);  
                b.setTitle("Alert");  
                b.setMessage(message);  
                b.setPositiveButton(android.R.string.ok,  
                        new AlertDialog.OnClickListener() {  
                            public void onClick(DialogInterface dialog, int which) {  
                                result.confirm();  
                            }  
                        });  
                b.setCancelable(false);  
                b.create();  
                b.show();  
                return true;  
            };  
  
            @Override  
            public boolean onJsConfirm(WebView view, String url, String message,  
                    final JsResult result) {  
                AlertDialog.Builder b = new AlertDialog.Builder(MyBrowser.this);  
                b.setTitle("Confirm");  
                b.setMessage(message);  
                b.setPositiveButton(android.R.string.ok,  
                        new AlertDialog.OnClickListener() {  
                            public void onClick(DialogInterface dialog, int which) {  
                                result.confirm();  
                            }  
                        });  
                b.setNegativeButton(android.R.string.cancel,  
                        new DialogInterface.OnClickListener() {  
                            public void onClick(DialogInterface dialog, int which) {  
                                result.cancel();  
                            }  
                        });  
                b.setCancelable(false);  
                b.create();  
                b.show();  
                return true;  
            };  
  
            @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 b = new AlertDialog.Builder(MyBrowser.this);  
                b.setTitle("Prompt");  
                b.setView(v);  
                b.setPositiveButton(android.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);  
                            }  
                        });  
                b.setNegativeButton(android.R.string.cancel,  
                        new DialogInterface.OnClickListener() {  
                            public void onClick(DialogInterface dialog, int which) {  
                                result.cancel();  
                            }  
                        });  
                b.setOnCancelListener(new DialogInterface.OnCancelListener() {  
                            public void onCancel(DialogInterface dialog) {  
                                result.cancel();  
                            }  
                        });  
                b.show();  
                return true;  
            };  
  
            @Override  
            public void onProgressChanged(WebView view, int newProgress) {  
                MyBrowser.this.getWindow().setFeatureInt(Window.FEATURE_PROGRESS, newProgress * 100);  
                super.onProgressChanged(view, newProgress);  
            }  
  
            @Override  
            public void onReceivedTitle(WebView view, String title) {  
                MyBrowser.this.setTitle(title);  
                super.onReceivedTitle(view, title);  
            }  
        });  
        go.setOnClickListener(new OnClickListener(){  
            public void onClick(View view) {  
                String url=text.getText().toString();  
                webView.loadUrl(url);  
            }  
        });  
        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);  
    }  
}  

最后修改:2011 年 08 月 07 日
卧槽,全是白嫖客,服务器不要钱吗?