android簡單網頁瀏覽器的開發

來源:互聯網
上載者:User

android簡單網頁瀏覽器的開發
 功能概要:
1.用WebView控制項實現簡單瀏覽器的開發
2.實現了網頁瀏覽器的簡易功能。
3.能夠瀏覽網頁中的所有內容。但不可使用視訊的播放。
4.瀏覽器載入速度快。
:

編程要點:
1.使用WebView控制項,用於網頁的顯示
2.設定WebView的屬性,使其支援更多的功能
3.當使用者點擊網頁中的連結,仍然用該網頁載入
4.設定許可權,使app具有上網許可權。
5.Activity的布局等。
[java]code

[java] 
package com.example.njupt.zhb.zhbwebbrowser; 
 
 
import android.os.Bundle; 
import android.annotation.SuppressLint; 
import android.app.Activity; 
import android.content.Intent; 
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; 
import android.widget.TextView; 
   /* 
    *@author: ZhengHaibo   
    *web:     blog.csdn.net/nuptboyzhb 
    *mail:    zhb931706659@126.com 
    *2012-8-31  Nanjing njupt 
    */ 
public class WebBrowser extends Activity implements OnClickListener{ 
    EditText url; 
    TextView mTitle; 
    WebView mWebView; 
    Button goButton; 
    Button backButton; 
    Button aboutButton; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_web_browser); 
        setTitle("WebBrowser Made by Zhenghaibo"); 
        setControl(); 
        setWebStyle(); 
    } 
    private void setControl() { 
        url=(EditText)findViewById(R.id.urltext); 
        mWebView=(WebView)findViewById(R.id.webshow); 
        goButton=(Button)findViewById(R.id.GoBtn); 
        backButton=(Button)findViewById(R.id.BackBtn); 
        aboutButton=(Button)findViewById(R.id.AboutBtn); 
        mTitle=(TextView)findViewById(R.id.WebTitle); 
        goButton.setOnClickListener(this); 
        backButton.setOnClickListener(this); 
        aboutButton.setOnClickListener(this); 
    } 
    @SuppressLint("SetJavaScriptEnabled") 
    private void setWebStyle() { 
        mWebView.getSettings().setJavaScriptEnabled(true); 
        mWebView.getSettings().setSupportZoom(true); 
        mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY); 
        mWebView.requestFocus(); 
        mWebView.loadUrl("http://blog.csdn.net/nuptboyzhb/"); 
        mWebView.setWebViewClient(new MyWebViewClient()); 
    } 
    @Override 
    public void onClick(View v) { 
        // TODO Auto-generated method stub 
        switch(v.getId()){ 
        case R.id.GoBtn: 
            String url_text; 
            String url_head = "http://"; 
            url_text=url.getText().toString(); 
            if(!url_text.contains("http://")){ 
                url_text=url_head.concat(url_text); 
                 
            } 
            mWebView.loadUrl(url_text); 
            mTitle.setText("you are browsing web: "+url_text); 
            break; 
        case R.id.BackBtn: 
            mWebView.goBack(); 
            break; 
        case R.id.AboutBtn: 
            Intent intent=new Intent(this,ActivityAbout.class); 
            startActivity(intent); 
           break; 
        } 
    } 
    class MyWebViewClient extends WebViewClient{ 
        @Override 
        public boolean shouldOverrideUrlLoading(WebView view,String url_){ 
            view.loadUrl(url_); 
            url.setText(url_); 
            mTitle.setText("you are browsing web: "+url_); 
            return true; 
        } 
    } 
} 

聯繫我們

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