Android網頁瀏覽器的開發

來源:互聯網
上載者:User

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical"    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" >    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="40dp"        android:gravity="center" >        <Button            android:id="@+id/btnEar"            android:layout_width="wrap_content"            android:layout_height="fill_parent"            android:layout_weight="1"            android:text="<<" />        <Button            android:id="@+id/btnPre"            android:layout_width="wrap_content"            android:layout_height="fill_parent"            android:layout_weight="1"            android:text="<" />        <Button            android:id="@+id/btnNext"            android:layout_width="wrap_content"            android:layout_height="fill_parent"            android:layout_weight="1"            android:text=">" />        <Button            android:id="@+id/btnLast"            android:layout_width="wrap_content"            android:layout_height="fill_parent"            android:layout_weight="1"            android:text=">>" />    </LinearLayout>    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="40dp" >        <EditText            android:id="@+id/edtUrl"            android:layout_width="wrap_content"            android:layout_height="fill_parent"            android:layout_weight="5"            android:ems="10"            android:singleLine="true" >            <requestFocus />        </EditText>        <Button            android:id="@+id/btnGo"            android:layout_width="wrap_content"            android:layout_height="fill_parent"            android:text="GO" />    </LinearLayout>    <WebView        android:id="@+id/webMain"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_weight="1" /></LinearLayout>

package com.hzhi.mybrowser;import android.os.Bundle;import android.app.Activity;import android.graphics.Bitmap;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.webkit.WebView;import android.webkit.WebViewClient;import android.widget.Button;import android.widget.EditText;public class MainActivity extends Activity implements OnClickListener{// 控制項Button btnEar;Button btnPre;Button btnNext;Button btnLast;EditText edtUrl;Button btnGo;WebView webMain;// URLString strUrl;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// 獲得控制項getCon();webMain.setWebViewClient(new MyWebViewClient());}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}// 獲得控制項public void getCon(){btnEar = (Button) findViewById(R.id.btnEar);btnPre = (Button) findViewById(R.id.btnPre);btnNext = (Button) findViewById(R.id.btnNext);btnLast = (Button) findViewById(R.id.btnLast);edtUrl = (EditText) findViewById(R.id.edtUrl);btnGo = (Button) findViewById(R.id.btnGo);webMain = (WebView) findViewById(R.id.webMain);btnEar.setOnClickListener(this);btnPre.setOnClickListener(this);btnNext.setOnClickListener(this);btnLast.setOnClickListener(this);edtUrl.setOnClickListener(this);btnGo.setOnClickListener(this);// 設定JavaScript可用webMain.getSettings().setJavaScriptEnabled(true);webMain.setScrollBarStyle(View.SCROLLBAR_POSITION_DEFAULT);}@Overridepublic void onClick(View v) {if (v==btnEar){if (webMain.canGoBackOrForward(-2)){webMain.goBackOrForward(-2);}}else if (v==btnPre){if (webMain.canGoBack()){webMain.goBack();}}else if (v==btnNext){if (webMain.canGoForward()){webMain.goForward();}}else if (v==btnLast){if (webMain.canGoBackOrForward(2)){webMain.goBackOrForward(2);}}else if (v==edtUrl){}// 下載網頁else if (v==btnGo){strUrl = edtUrl.getText().toString();webMain.loadUrl("http://" + strUrl);}}// WebViewClient的處理類class MyWebViewClient extends WebViewClient {@Overridepublic void doUpdateVisitedHistory(WebView view, String url, boolean isReload) {btnPre.setEnabled(webMain.canGoBack());btnNext.setEnabled(webMain.canGoForward());}@Overridepublic void onPageFinished(WebView view, String url) {if (webMain.getTitle() != null) {MainActivity.this.setTitle(webMain.getTitle());}}@Overridepublic void onPageStarted(WebView view, String url, Bitmap favicon) {MainActivity.this.setTitle(url);edtUrl.setText(url);btnPre.setEnabled(webMain.canGoBack());btnNext.setEnabled(webMain.canGoForward());}}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.