WebView浏览网页

使用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() {
			
			@Override
			public 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>


猜你喜欢

转载自blog.csdn.net/huazicomeon/article/details/59733197
今日推荐