使用android内置浏览器打开网页

<pre name="code" class="java">package com.example.exp14_10_1;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.text.method.KeyListener;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
	private WebView wv;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		wv = (WebView) findViewById(R.id.webView1);
		wv.getSettings().setJavaScriptEnabled(true);
		wv.setWebChromeClient(new WebChromeClient());
        wv.setWebViewClient(new WebViewClient());
       // wv.setWebViewClient(new WebViewClient());////如果不适用该句代码将使用内置浏览器
		
		
		Button back = (Button) findViewById(R.id.back);
		Button forward = (Button) findViewById(R.id.forward);
		Button GO = (Button) findViewById(R.id.go);
		final EditText urlText = (EditText) findViewById(R.id.editTextUrl);

		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				wv.goBack();
			}
		});
		forward.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				wv.goForward();
			}
		});
		GO.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

		 String urlStr = urlText.getText().toString();

				if (!"".equals(urlStr)) {
					openBrowser(urlStr);

				} else {
					openBrowser(urlStr);
					showDialog();
				}

			}
		});

		urlText.setOnKeyListener(new OnKeyListener() {

			public boolean onKey(View v, int keyCode, KeyEvent event) {
				// TODO Auto-generated method stub
				if (keyCode == KeyEvent.KEYCODE_ENTER) {
			 String urlStr2 = urlText.getText().toString();

					if (!"".equals(urlStr2)) {
						openBrowser(urlStr2);
                       return true;///不让文本框换行
					} else {
						openBrowser(urlStr2);
						showDialog();
						return true;//不让文本框换行,但showDialog显示了两次,为何呢??
					}

				}
				///return true;////这会导致文本内容无法删除,删除没反应
				return false;
			}
		});
	}// /onCreate

	private void openBrowser(String url) {
		Toast.makeText(MainActivity.this, "正在加载" + url, Toast.LENGTH_SHORT)
				.show();
		wv.loadUrl(url);

	}

	private void showDialog() {
		new AlertDialog.Builder(MainActivity.this).setTitle("浏览器")
				.setMessage("请输入网址!")
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						Log.d("webview", "单击确定按钮");

					};
				}).show();// //new DialogInterface.OnClickListener()

	}// /showDialog
}


 
 
<pre name="code" class="html"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="http://192.168.1.105:8080/bbs"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Go" />

    <WebView
        android:id="@+id/webView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>
使用android内置浏览器打开网页_第1张图片

 
 
 
 


你可能感兴趣的:(使用android内置浏览器打开网页)