博主第一次发博客,如果有什么不对的希望大家指出,博主会修改的,谢谢大家支持^ _ ^
android利用浏览器访问网络有两种方法,一android默认浏览器浏览,一种利用WebView实现一个浏览器app;楼主在这里讲的是第二种方法,比较简单的;
首先,我们要在声明文件中加入访问权限
<uses-permission android:name="android.permission.INTERNET"/>
然后再到布局了,不多说直接贴代码;
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
//地址栏提示作用
<AutoCompleteTextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:id="@+id/editText1"
android:layout_weight="1" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="5"
android:id="@+id/button1"
android:text="go"/>
</LinearLayout>
<WebView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/webview"/>
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="back"/>
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="go"/>
<Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="home"/>
<Button
android:id="@+id/button5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="exit"/>
</LinearLayout>
</LinearLayout>
然后就是MainActicity.java文件源码;
package com.example.mywebview;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements OnClickListener {
private WebView webview;
private AutoCompleteTextView editText;//地址栏
private String[] mstr={"http://www.baidu.com/","baidu.com"};//提示信息
private ArrayAdapter<String> madapter;//定义一个适配器用来加载mstr数据到AutoCompleteTextView
private Button button1,button2,button3,button4,button5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webview=(WebView)findViewById(R.id.webview);
editText=(AutoCompleteTextView)findViewById(R.id.editText1);
button1=(Button)findViewById(R.id.button1);//访问按钮
button2=(Button)findViewById(R.id.button2);//返回按钮
button3=(Button)findViewById(R.id.button3);//前进按钮
button4=(Button)findViewById(R.id.button4);//主页按钮
button5=(Button)findViewById(R.id.button5);//退出按钮
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
button4.setOnClickListener(this);
button5.setOnClickListener(this);
madapter=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,mstr);//创建adapter对象
editText.setAdapter(madapter);
webview.loadUrl("http://www.baidu.com/");//webview利用loadUrl方法访问网页
WebSettings settings=webview.getSettings();//WebSettings对象用来处理JavaScript是否加载到webview上
settings.setJavaScriptEnabled(true);//设置true,表示加载到webview上,否则系统会默认调用系统浏览器浏览网页
settings.setLoadsImagesAutomatically(true);//有图浏览
webview.setWebViewClient(new MyWebViewClient());//设置MyWebViewClient处理访问网页跳转问题;请跳到MyWebViewClient类看代码
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
String url=editText.getText().toString().trim();
conn(url);
break;
case R.id.button2:
webview.goBack();
break;
case R.id.button3:
webview.goForward();
break;
case R.id.button4:
webview.loadUrl("http://www.baidu.com/");
webview.clearHistory();
break;
case R.id.button5:
finish();
break;
}
}
//设置一个类继承WebViewClient,重写shouldOverrideUrlLoading方法
private class MyWebViewClient extends WebViewClient{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);//访问webView拿到的网址
editText.setText(url);//将网址设置到地址栏上
return true;
}
}
//http://www.bilibili.com/mobile/index.html
private void conn(String url){
webview.loadUrl(url);
editText.setText(url);
}
}