迷你瀏覽器——WebView學習

來源:互聯網
上載者:User

標籤:

MiniBrowser.java

public class MiniBrowser extends Activity {EditText url;WebView showWebView;Button searchButton;String urlStr = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);url = (EditText) findViewById(R.id.url);showWebView = (WebView) findViewById(R.id.show);searchButton = (Button) findViewById(R.id.searchButton);//真機測試時,保證在當前的Activity顯示頁面,而非內建瀏覽器顯示showWebView.setWebViewClient(new WebViewClient(){@Overridepublic boolean shouldOverrideUrlLoading(WebView view, String url) {// TODO Auto-generated method stub//用WebView 載入頁面view.loadUrl(url);return true;}});//enable JavaScript showWebView.getSettings().setJavaScriptEnabled(true);searchButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stuburlStr = url.getText().toString();showWebView.loadUrl(urlStr);}});}@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {// TODO Auto-generated method stub//按後退鍵可以跳到上級頁面if(keyCode == KeyEvent.KEYCODE_BACK){showWebView.goBack();return true;}return super.onKeyDown(keyCode, event);}}
mail.xml 布局檔案

<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="wrap_content"    android:layout_height="match_parent"    tools:context=".MiniBrowser" >    <EditText         android:id="@+id/url"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:singleLine="true"        android:layout_toLeftOf="@+id/searchButton"        android:layout_alignTop="@+id/searchButton"        android:layout_alignBottom="@+id/searchButton" />    <Button        android:id="@+id/searchButton"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentRight="true"        android:text="@string/search" />   <WebView     android:id="@+id/show"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:layout_below="@id/searchButton"/> </RelativeLayout>


Manife檔案設定允許上網

<uses-permission android:name="android.permission.INTERNET"/>


在真機的測試結果







迷你瀏覽器——WebView學習

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.