1 packagecom.xxx.webview;2
3 importjava.util.Timer;4
5 importjava.util.Timer;6 importjava.util.TimerTask;7 importjava.util.regex.Matcher;8 importjava.util.regex.Pattern;9
10 importandroid.app.Activity;11 importandroid.graphics.Bitmap;12 importandroid.os.Bundle;13 importandroid.os.Handler;14 importandroid.os.Message;15 importandroid.view.KeyEvent;16 importandroid.view.Menu;17 importandroid.view.View;18 importandroid.view.View.OnClickListener;19 importandroid.webkit.WebView;20 importandroid.webkit.WebViewClient;21 importandroid.widget.ArrayAdapter;22 importandroid.widget.AutoCompleteTextView;23 importandroid.widget.Button;24 importandroid.widget.Toast;25
26 public class MainActivity extendsActivity {27
28 AutoCompleteTextView url;29 WebView show;30
31 String[] urlArray = newString[]32 {33 "www.baidu.com", "www.bing.com"
34 };35
36 @Override37 public voidonCreate(Bundle savedInstanceState) {38 super.onCreate(savedInstanceState);39 setContentView(R.layout.activity_main);40
41 final Activity activity = this;42
43 show =(WebView)findViewById(R.id.show);44 show.getSettings().setJavaScriptEnabled(true);45 show.getSettings().setBuiltInZoomControls(true);46 show.setWebViewClient(newWebViewClient() {47 public booleanshouldOverrideUrlLoading(WebView view, String strUrl) {48 view.loadUrl(strUrl);49 url.setText(strUrl);50 return false;51 }52
53 public voidonPageStarted(WebView view, String strUrl, Bitmap favicon) {54 super.onPageStarted(view, strUrl, favicon);55 url.setText(strUrl);56 }57
58 public voidonPageFinished(WebView view, String strUrl) {59 }60
61 public void onReceivedError(WebView view, interrorCode, String description, String failingUrl) {62 Toast.makeText(activity, "Oh no! " +description, Toast.LENGTH_SHORT).show();63 }64 });65
66
67 url =(AutoCompleteTextView)findViewById(R.id.url);68 ArrayAdapter urlAdapter = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, urlArray);69 url.setAdapter(urlAdapter);70 url.setOnKeyListener(newView.OnKeyListener()71 {72 public boolean onKey(View v, intkeyCode, KeyEvent ev)73 {74 if (keyCode ==KeyEvent.KEYCODE_ENTER)75 {76 String strUrl =url.getText().toString();77
78 Pattern p = Pattern.compile("http://([\\w-]+\\.)+[\\w-]+(/[\\w-\\./?%=]*)?");79 Matcher m =p.matcher(strUrl);80 if (!m.find())81 {82 strUrl = "http://" +strUrl;83 }84
85 show.loadUrl(strUrl);86
87 return true;88 }89
90 return false;91 }92 });93
94 //button
95 final Button backBtn =(Button)findViewById(R.id.back);96 final Button forwardBtn =(Button)findViewById(R.id.forward);97 Button refreshBtn =(Button)findViewById(R.id.refresh);98 backBtn.setEnabled(false);99 forwardBtn.setEnabled(false);100
101 backBtn.setOnClickListener(newOnClickListener()102 {103 public voidonClick(View v)104 {105 show.goBack();106 }107 });108
109 forwardBtn.setOnClickListener(newOnClickListener()110 {111 public voidonClick(View v)112 {113 //TODO
114 show.goForward();115 }116 });117
118 refreshBtn.setOnClickListener(newOnClickListener()119 {120 public voidonClick(View v)121 {122 //TODO
123 String strUrl =url.getText().toString();124 show.loadUrl(strUrl);125 }126 });127
128 final Handler handler = newHandler()129 {130 @Override131 public voidhandleMessage(Message msg)132 {133 if (msg.what == 0x1111)134 {135 //whether can go back
136 if(show.canGoBack())137 {138 backBtn.setEnabled(true);139 }140 else
141 {142 backBtn.setEnabled(false);143 }144
145 //whether can go forward
146 if(show.canGoForward())147 {148 forwardBtn.setEnabled(true);149 }150 else
151 {152 forwardBtn.setEnabled(false);153 }154 }155
156 super.handleMessage(msg);157 }158 };159
160 //create thread to change button states
161 new Timer().schedule(newTimerTask()162 {163 public voidrun()164 {165 Message msg = newMessage();166 msg.what = 0x1111;167 handler.sendMessage(msg);168 }169 }, 0, 100);170 }171
172 @Override173 public boolean onKeyDown(intkeyCode, KeyEvent event) {174
175 if ((keyCode == KeyEvent.KEYCODE_BACK) &&show.canGoBack())176 {177 show.goBack();178 return true;179 }180
181 return super.onKeyDown(keyCode, event);182 }183
184 }