(1)使用WebView则需要在布局文件中使用相应的布局控件
布局文件如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <RelativeLayout android:id="@+id/web_title_relativelayout" android:layout_width="match_parent" android:layout_height="50dp" android:layout_alignLeft="@+id/web_title_relativelayout" > <Button android:id="@+id/back" android:layout_width="60dp" android:layout_height="40dp" android:text="返回" /> <TextView android:id="@+id/tv_url" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="TextView" /> <Button android:id="@+id/refresh" android:layout_width="60dp" android:layout_height="40dp" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:text="刷新" /> </RelativeLayout> <!-- WebView的使用 --> <WebView android:id="@+id/webView1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:layout_below="@id/web_title_relativelayout" /> </RelativeLayout>
MainActivity.java的代码如下:
package com.lc.webview_test1; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.webkit.WebChromeClient; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; /** * 使用WebView要添加使用网络的权限 */ public class MainActivity extends Activity implements OnClickListener { private WebView webView; private Button backButton; private Button refreshButton; private TextView tv_url; private static final String URL_VALUE = "http://www.baidu.com/"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); webView = (WebView) this.findViewById(R.id.webView1); backButton = (Button) this.findViewById(R.id.back); refreshButton = (Button) this.findViewById(R.id.refresh); tv_url = (TextView) this.findViewById(R.id.tv_url); // 设置监听事件 backButton.setOnClickListener(this); refreshButton.setOnClickListener(this); // 如果只是用这句话的话,这样的话默认的加载系统自带的游览器打开该url // webView.loadUrl("http://www.baidu.com/"); webView.loadUrl(URL_VALUE); /* * 为避免使用系统自带的游览器,我们需要重写一下的方法 */ webView.setWebChromeClient(new WebChromeClient() { @Override public void onReceivedTitle(WebView view, String title) { tv_url.setText(title);// 设置标题 super.onReceivedTitle(view, title); } }); webView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return super.shouldOverrideUrlLoading(view, url); } /** * 获得异常的界面,展示错误页面的 */ @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { super.onReceivedError(view, errorCode, description, failingUrl); view.loadUrl("file:///android_asset/404.html"); } }); } /** * 按钮点击事件的操作方法 */ @Override public void onClick(View v) { switch (v.getId()) { case R.id.back: Toast.makeText(MainActivity.this, "back...", 1).show(); finish(); break; case R.id.refresh: Toast.makeText(MainActivity.this, "refresh...", 1).show(); webView.reload();// 重新加载 break; default: break; } } }
演示效果如下:
时间: 2024-10-11 00:45:10