WebView浏览网页

来源:互联网 发布:合唱指挥的作用知乎 编辑:程序博客网 时间:2024/05/14 07:45

使用WebView可以在APP中调用浏览器,在地址栏输入网址(地址需要带http://www.),点击按钮即可搜索。

Java文件

package com.example.test;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.webkit.WebView;import android.widget.Button;import android.widget.EditText;public class MainActivity extends Activity{EditText urlEditText;WebView showView;Button button;public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);urlEditText = (EditText)findViewById(R.id.url);showView = (WebView)findViewById(R.id.show);button = (Button)findViewById(R.id.button);button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {String urlStr = urlEditText.getText().toString();showView.loadUrl(urlStr);}});}}

布局文件

<?xml version="1.0" encoding="UTF-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent" xmlns:android1="http://schemas.android.com/apk/res/android">    <EditText         android:id="@+id/url"        android:layout_width="match_parent"        android:layout_height="wrap_content"        />    <Button        android:id="@+id/button"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Button" />    <WebView        android:id="@+id/show"        android:layout_width="match_parent"        android:layout_height="match_parent"        /></LinearLayout>


0 0
原创粉丝点击