MiniBrowser.Java
- public class MiniBrowser extends Activity {
- EditText url;
- WebView showWebView;
- Button searchButton;
- String urlStr = null;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- url = (EditText) findViewById(R.id.url);
- showWebView = (WebView) findViewById(R.id.show);
- searchButton = (Button) findViewById(R.id.searchButton);
- //真机测试时,保证在当前的Activity显示页面,而非自带浏览器显示
- showWebView.setWebViewClient(new WebViewClient(){
- @Override
- public boolean shouldOverrideUrlLoading(WebView view, String url) {
- // TODO Auto-generated method stub
- //用WebView 载入页面
- view.loadUrl(url);
- return true;
- }
- });
- //enable JavaScript
- showWebView.getSettings().setJavaScriptEnabled(true);
- searchButton.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- urlStr = url.getText().toString();
- showWebView.loadUrl(urlStr);
- }
- });
- }
- @Override
- public boolean onKeyDown(int keyCode, KeyEvent event) {
- // TODO Auto-generated method stub
- //按后退键可以跳到上级页面
- if(keyCode == KeyEvent.KEYCODE_BACK){
- showWebView.goBack();
- return true;
- }
- return super.onKeyDown(keyCode, event);
- }
- }
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="wrap_content"
- android:layout_height="match_parent"
- tools:context=".MiniBrowser" >
- <EditText
- android:id="@+id/url"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:singleLine="true"
- android:layout_toLeftOf="@+id/searchButton"
- android:layout_alignTop="@+id/searchButton"
- android:layout_alignBottom="@+id/searchButton" />
- <Button
- android:id="@+id/searchButton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true"
- android:text="@string/search" />
- <WebView
- android:id="@+id/show"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_below="@id/searchButton"/>
- </RelativeLayout>
- <uses-permission android:name="android.permission.INTERNET"/>