package gzdewei.com.android; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.webkit.WebView; import android.webkit.WebViewClient; public class EJgrzActivity extends Activity { /** Called when the activity is first created. */ private WebView webview; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.main); //新建一个WEB浏览器 webview = new WebView(this); //开启js支持 webview.getSettings().setJavaScriptEnabled(true); //网页自适应屏幕开关 //webview.getSettings().setUseWideViewPort(true); webview.loadUrl("http://xx.xx.com/login"); //开始 webview.setWebViewClient(new WebViewClient(){ public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } }); setContentView(webview); } @Override public boolean onCreateOptionsMenu(Menu menu) { menu.add(Menu.NONE, Menu.FIRST + 2, 1, "后退").setIcon( android.R.drawable.ic_media_previous); menu.add(Menu.NONE, Menu.FIRST + 1, 2, "前进").setIcon( android.R.drawable.ic_media_next); menu.add(Menu.NONE, Menu.FIRST + 3, 3, "刷新").setIcon( android.R.drawable.ic_menu_directions); menu.add(Menu.NONE, Menu.FIRST + 4, 4, "退出").setIcon( android.R.drawable.ic_delete); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case Menu.FIRST + 2: //Toast.makeText(this, "删除菜单被点击了", Toast.LENGTH_LONG).show(); webview.goBack(); break; case Menu.FIRST + 1: //Toast.makeText(this, "删除菜单被点击了", Toast.LENGTH_LONG).show(); webview.goForward(); break; case Menu.FIRST + 3: webview.reload(); break; case Menu.FIRST + 4: finish(); break; } return false; } @Override public void onOptionsMenuClosed(Menu menu) { //Toast.makeText(this, "选项菜单关闭了", Toast.LENGTH_LONG).show(); } @Override public boolean onPrepareOptionsMenu(Menu menu) { //Toast.makeText(this,"选项菜单显示之前onPrepareOptionsMenu方法会被调用,你可以用此方法来根据打当时的情况调整菜单",Toast.LENGTH_LONG).show(); // 如果返回false,此方法就把用户点击menu的动作给消费了,onCreateOptionsMenu方法将不会被调用 return true; } }
原文:http://www.cnblogs.com/blackice/p/5418823.html