activity.xlm
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/editText1"
android:layout_weight="5"/>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="登陆"
android:textSize="20dp"
android:textStyle="bold"
android:id="@+id/button5"
android:textColor="#000000"
android:layout_weight="1"/>
</LinearLayout>
<WebView
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="@+id/web1"
android:layout_weight="16.5"></WebView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight="2">
<Button
android:layout_width="wrap_content"
android:id="@+id/button1"
android:layout_height="wrap_content"
android:text="←"
android:textColor="#000000"
android:textStyle="bold"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button2"
android:text="主页"
android:textStyle="bold"
android:textColor="#000000"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button3"
android:text="→"
android:textStyle="bold"
android:textColor="#000000"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="exit"
android:textStyle="bold"
android:textColor="#000000"
android:id="@+id/button4"/>
</LinearLayout>
</LinearLayout>
Mainactivity.java
package com.example.wuzuo.web_web;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private WebView webView;
private String url=null;
private Button button_1;
private Button button_2;
private Button button_3;
private Button button_4;
private Button button_5;
private EditText editText_1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.activity_main);
setProgressBarIndeterminate(true);
editText_1= (EditText) findViewById(R.id.editText1);
button_1= (Button) findViewById(R.id.button1);
button_2= (Button) findViewById(R.id.button2);
button_3= (Button) findViewById(R.id.button3);
button_4= (Button) findViewById(R.id.button4);
button_5= (Button) findViewById(R.id.button5);
webView= (WebView) findViewById(R.id.web1);
button_1.setOnClickListener(this);
button_2.setOnClickListener(this);
button_3.setOnClickListener(this);
button_4.setOnClickListener(this);
button_5.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.button5:
url="http://"+editText_1.getText().toString();//获取editText当前网址
url=url.replace(" ","");//网址加上引号
startreadurl(url);
break;
case R.id.button1:
if(webView.canGoBack())
{
webView.goBack();
} else {
finish();
}
break;
case R.id.button2:
startreadurl("http://www.baidu.com");
break;
case R.id.button3:
if(webView.canGoForward())
{
webView.goForward();
}
break;
case R.id.button4:
finish();
break;
}
}
private void startreadurl(String url) {
webView.loadUrl(url);
webView.setWebViewClient(new WebViewClient() {//直接在页面打开,不利用第三方浏览器
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
/*启动javascrip功能*/
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); // web加载页面优先使用缓存加载
webView.setWebChromeClient(new WebChromeClient()//页面加载时有进度显示
{
@Override
public void onProgressChanged(WebView view, int newProgress) {
setTitle("页面加载" + newProgress + "%");
if(newProgress==100)
{
closeprogressBar();
}
else
{
openprogressBar(newProgress);
}
super.onProgressChanged(view, newProgress);
}
});
}
protected void openprogressBar(int x)//进度条关闭
{
setProgressBarIndeterminateVisibility(true);
setProgress(x);
}
protected void closeprogressBar()//进度条打开
{
setProgressBarIndeterminateVisibility(false);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) { // 改写物理按键 返回键的逻辑
if(keyCode==KeyEvent.KEYCODE_BACK)
{
if(webView.canGoBack())
{
webView.goBack();
return true;
}
else
{
finish();
}
}
return super.onKeyDown(keyCode, event);
}
}
本文介绍了一个简单的Android Web浏览器应用程序的设计与实现过程。该应用具备基本的网页浏览功能,包括输入URL进行网页加载、前进后退浏览历史记录、返回主页及退出操作等。文章详细展示了XML布局文件与MainActivity.java代码,并介绍了WebView组件及其常用方法。

被折叠的 条评论
为什么被折叠?



