Android百議程序 開篇章:Intent開啟網頁

來源:互聯網
上載者:User

標籤:android   style   blog   http   io   ar   os   使用   sp   

學習一下人家100日寫100個網頁的做法,我也用100日寫100個完整的Android程式。

這些程式的最基本要求:

1 完整性-每個程式都必須是獨立可啟動並執行

2 不重複性-所用的重點知識點都不一樣


開篇章:

--本章參考書本:Hello Android

編寫一個簡單的頁面,如下,圖1:


在文字框輸入網址,圖2:


點擊按鈕GO,然後就可以開啟這個網站了, 圖3:



步驟:

一  首先,建立一個項目,具體參數設定可以參照我的項目設定,如下:


主要看

1 src下的Browser.java:主要的java邏輯代碼

2 layout下的activity_browser.xml,xml寫的布局

3 values的strings.xml會需要定義一些字元

4 AndroidManifest.xml是軟體的定義檔案了


二 設定布局layout,開啟activity_broswser.xml

添加代碼:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal"    tools:context="su.bi.browerserintent.Browser" >    <EditText        android:id="@+id/url_field"        android:layout_width="0dip"        android:layout_height="wrap_content"        android:layout_weight="1.0"        android:imeOptions="actionGo"        android:inputType="textUri"        android:lines="1" />    <Button        android:id="@+id/go_button"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/go_button" /></LinearLayout>


知識點:
1 使用LinearLayout布局,添加一個EditText就是圖片的第一有一個下橫線的框,接受網址的輸入注意其中的layout_width的設定,為零,那麼後面的layout_weight="1.0"就使得框格擴充所有該行餘下的空間了,故此直接設定layout_width為零就可以了。

2 其中的參數imeOptions="actionGo"和imputType="textUri"是告訴Android使用的軟鍵盤應該是帶".com"和"/"和有Go按鍵的,按下這個按鍵就可以直接跳轉到指定網頁。

就這樣就做出1的介面了


二 邏輯代碼 Browser.java

接下來就是驅動介面工作的邏輯代碼

1 設定好處理輸入和按鍵功能

    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_browser);                //Get a handle to all user interface elements        urlText = (EditText)findViewById(R.id.url_field);        goButton = (Button)findViewById(R.id.go_button);                //Set up Handlers        goButton.setOnClickListener(new OnClickListener() {        public void onClick(View v) {        openBrowser();        }        });                urlText.setOnKeyListener(new OnKeyListener() {        public boolean onKey(View v, int keyCode, KeyEvent event) {        if (equ(keyCode, KeyEvent.KEYCODE_ENTER)) {        openBrowser();        return true;        }        return false;        }        });    }
其中的openBrowser函數是下面自訂的。

goButton這個按鍵響應點擊事件,故此使用setOnClickListener(new OnClickListener())設定好OnClickListener對象監聽這個按鍵,這裡是Java專屬的機制了,直接在new 後面寫一個OnClickListener的繼承類,重寫onClick函數。 C++就不能這麼寫了。

urlText是EditText的id,使用setOnKeyListener設定好對象監聽,如果按下ENTER的時候就響應openBrowser函數,當然現在的觸屏手機一般都沒有按鍵了,不過有軟鍵盤。


openBroser函數定義:

private void openBrowser() {<span style="white-space:pre"></span>Uri uri = Uri.parse(urlText.getText().toString());<span style="white-space:pre"></span>Intent intent = new Intent(Intent.ACTION_VIEW, uri);<span style="white-space:pre"></span>startActivity(intent);}
建立一個Intent,使用瀏覽器開啟這個網址。


到此為止,整個程式就可以運行了。


但是現在問題來了:

如果輸入blog.csdn.net/kenden23,那麼就會程式崩潰的。這是為什麼呢?


重新輸入:http://blog.csdn.net/kenden23就可以正常工作了。

哦,原來必須是以http://開頭的文本才可以正常工作,

這就需要小小處理一下了,這就是演算法排上用場的時候啦,字串處理問題--有點牛刀宰雞的感覺了。

重新定義openBrwoser函數:

private static final String HTTPHEAD = "http://";public static<T> boolean equ(T a, T b) {return a == b;}    private void openBrowser() {    String address = urlText.getText().toString();    address = checkHttpAddress(address);    Uri uri = Uri.parse(address);        Intent intent = new Intent(Intent.ACTION_VIEW, uri);        startActivity(intent);    }        private String checkHttpAddress(String address) {    int len = HTTPHEAD.length();    len = len < address.length()? len : address.length();    String str = address.substring(0, len);    if (equ(str, HTTPHEAD)) {    return address;    }    return HTTPHEAD + address;    }
小技巧:自訂equ的好處是不會把==錯寫成=了。壞處?多寫點代碼。哈哈。

主要是checkHttpAddress這個函數起作用,就是判斷目前使用者的輸入是否帶"http://",如果沒帶,那麼就自動加上,如果帶了,就不用管了。

注意演算法的功力,達到無bug。無論使用者輸入什麼字串,都不會程式崩潰了,不過也許會找不到網站,如:


這樣工作是正常的。故此大功告成!



Android百議程序 開篇章:Intent開啟網頁

聯繫我們

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