日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > HTML >内容正文

HTML

android 代码浏览,Webview实现android简单的浏览器实例代码

發布時間:2025/3/21 HTML 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 代码浏览,Webview实现android简单的浏览器实例代码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

WebView是Android中一個非常實用的組件,它和Safai、Chrome一樣都是基于Webkit網頁渲染引擎,可以通過加載HTML數據的方式便捷地展現軟件的界面,下面通過本文給大家介紹Webview實現android簡單的瀏覽器實例代碼。

實現了瀏覽器的返回 前進 主頁 退出 輸入網址的功能

注釋的很清楚啦 就不多說了

首先是布局文件

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

android:orientation="horizontal"

android:layout_width="fill_parent"

android:layout_height="wrap_content" >

android:id="@+id/et_url"

android:layout_width="320dp"

android:layout_height="wrap_content" />

android:id="@+id/btn_login"

android:layout_height="wrap_content"

android:layout_width="wrap_content"

android:text="登錄"

/>

android:layout_weight="2"

android:id="@+id/webView"

android:layout_width="fill_parent"

android:layout_height="fill_parent" />

android:layout_weight="7.5"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="horizontal"

android:background="#000000"

>

android:id="@+id/btn_back"

android:layout_weight="1"

android:layout_height="wrap_content"

android:layout_width="wrap_content"

android:text="←"

/>

android:id="@+id/btn_menu"

android:layout_weight="1"

android:layout_height="wrap_content"

android:layout_width="wrap_content"

android:text="主頁"

/>

android:id="@+id/btn_forward"

android:layout_weight="1"

android:layout_height="wrap_content"

android:layout_width="wrap_content"

android:text="→"

/>

android:id="@+id/btn_exit"

android:layout_weight="1"

android:layout_height="wrap_content"

android:layout_width="wrap_content"

android:text="exit"

/>

MainActivity

package com.example.webview;

import android.os.Bundle;

import android.app.Activity;

import android.view.KeyEvent;

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.Button;

import android.widget.EditText;

public class MainActivity extends Activity implements OnClickListener {

private String url = null;

private WebView webView;

private EditText et_url;

private Button btn_login;

private Button btn_back;

private Button btn_exit;

private Button btn_forward;

private Button btn_menu;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

// 窗口進度條

requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

setContentView(R.layout.activity_main);

setProgressBarIndeterminate(true);

webView = (WebView) findViewById(R.id.webView);

et_url = (EditText) findViewById(R.id.et_url);

btn_login = (Button) findViewById(R.id.btn_login);

btn_back = (Button) findViewById(R.id.btn_back);

btn_exit = (Button) findViewById(R.id.btn_exit);

btn_forward = (Button) findViewById(R.id.btn_forward);

btn_menu = (Button) findViewById(R.id.btn_menu);

// 對五個按鈕添加點擊監聽事件

btn_login.setOnClickListener(this);

btn_back.setOnClickListener(this);

btn_exit.setOnClickListener(this);

btn_forward.setOnClickListener(this);

btn_menu.setOnClickListener(this);

}

// btn_login的觸發事件 點擊后 webView開始讀取url

protected void startReadUrl(String url) {

// TODO Auto-generated method stub

// webView加載web資源

webView.loadUrl(url);

// 覆蓋webView默認通過系統或者第三方瀏覽器打開網頁的行為

// 如果為false調用系統或者第三方瀏覽器打開網頁的行為

webView.setWebViewClient(new WebViewClient() {

@Override

public boolean shouldOverrideUrlLoading(WebView view, String url) {

// TODO Auto-generated method stub

// webView加載web資源

view.loadUrl(url);

return true;

}

});

// 啟用支持javascript

WebSettings settings = webView.getSettings();

settings.setJavaScriptEnabled(true);

// web加載頁面優先使用緩存加載

settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);

// 當打開頁面時 顯示進度條 頁面打開完全時 隱藏進度條

webView.setWebChromeClient(new WebChromeClient() {

@Override

public void onProgressChanged(WebView view, int newProgress) {

// TODO Auto-generated method stub

setTitle("本頁面已加載" + newProgress + "%");

if (newProgress == 100) {

closeProgressBar();

} else {

openProgressBar(newProgress);

}

super.onProgressChanged(view, newProgress);

}

});

}

// 打開進度條

protected void openProgressBar(int x) {

// TODO Auto-generated method stub

setProgressBarIndeterminateVisibility(true);

setProgress(x);

}

// 關閉進度條

protected void closeProgressBar() {

// TODO Auto-generated method stub

setProgressBarIndeterminateVisibility(false);

}

// 改寫物理按鍵 返回鍵的邏輯

@Override

public boolean onKeyDown(int keyCode, KeyEvent event) {

// TODO Auto-generated method stub

if (keyCode == KeyEvent.KEYCODE_BACK) {

if (webView.canGoBack()) {

// 返回上一頁面

webView.goBack();

return true;

} else {

// 退出程序

finish();

}

}

return super.onKeyDown(keyCode, event);

}

// 對按鈕事件的處理

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

switch (v.getId()) {

case R.id.btn_login:

url = "http://" + et_url.getText().toString();

url = url.replace(" ", "");

startReadUrl(url);

break;

case R.id.btn_back:

if (webView.canGoBack()) {

webView.goBack();

} else {

finish();

}

break;

case R.id.btn_forward:

if (webView.canGoForward()) {

webView.goForward();

}

break;

case R.id.btn_exit:

finish();

break;

case R.id.btn_menu:

startReadUrl("http://www.baidu.com");

break;

}

}

}

最后不要忘記在AndroidManifest.xml文件中配置網絡訪問的權限

以上內容給大家介紹了Webview實現android簡單的瀏覽器實例代碼,希望對大家有所幫助!

總結

以上是生活随笔為你收集整理的android 代码浏览,Webview实现android简单的浏览器实例代码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。