Pro Android學習筆記(十四):使用者介面和控制(2):Text類控制

來源:互聯網
上載者:User
TextView

TextView之前已經使用過很多,直接顯示,比較簡單。但是我們可以同“autoLink”屬性,使使用者可以點擊一個網路連接、電話號碼、郵箱地址、地圖地址,通過系統應用開啟它們。除此之外,還有其他的font屬性,minLines,maxLines等等,都很好理解,可以去閱讀Android學習筆記系列。

例子1:在XML中設定autoLink屬性

我們在xml中設定<textview ... android:autoLink="email|web" />,可以設定的autoLink的有all,email,map,none(這是預設值,表示不進行自動連結),phone,web。

例子2:在代碼中設定autoLink屬性

tv.setAutoLinkMask(Linkify.ALL);// 設定全部的自動連結
tv.setAutoLinkMask(Linkify.PHONE_NUMBERS |Linkify.WEB_URLS); //設定web和phone
tv.setText(……); //需要注意,設定屬性應該在setText()之前,否則該屬性不能作用在已設定的text上

在某些情況下,例如只要求臨時起作用,我們可能需要在設定text之後,才加上link屬性,可用下面的方法。Linkify.addLinkes()還可用於Spannable控制項。

tv.setText("My Phone : 87654321"); 
Linkify.addLinks(tv, Linkify.ALL); //需要注意,將Linkify.addLinkes()放置在setText()之前是不起作用的

EditText

EditText是TextView的子類。EditText沒有互連網上的功能那麼強,例如編輯部落格可以輸入圖片,但是對於手機也提供豐富功能。具體可以看reference,這裡介紹2個比較有趣的功能。

hint。可以在EditText中用灰色字型顯示提示,當使用者點擊輸入時,該提示消失。在XML中,如下設定:

<EditText .... android:hint="@string/ui_text_et_hint"/>

EditText繼承了TextView的一個重要屬性是inputType。我們可以設定textAutoCorrect來檢測拼字錯誤,可以通過textCapWords來自動設為每個單詞的第一個字母為大寫。還可以設定要求輸入格式,例如textUri,textEmailAddress,textPassword等等。

<EditText .... android:inputType="textPersonName|textCapWords"/>

在老的版本中,很多屬性都被移到inputType中設定,如果我們在xml中設定了inputType,這些到期的屬性將會不起作用,例如android:capitalize。

新老版本中需要注意多行的問題,在老版本中,預設是多行的,及當行不能全部顯示,自動在下一行顯示,如要單行顯示則需要設定android:singleLine。但是當XML的屬性設定了inputType,則預設是單行的,如果要多行,則需設定android:inputType="textMultiLine"。

AutoCompleteTextView

自動完成的control例子如上。XML如下:

<TextView ...... /> 
<AutoCompleteTextView android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/ui_text_auto"/>

代碼如下:

AutoCompleteTextView actv =  (AutoCompleteTextView)findViewById(R.id.ui_text_auto);
//建立一個一個adpater類,包含有建議項,和顯示建議的control
ArrayAdapter<String> aa = new ArrayAdapter<String>(this, 
                                                   android.R.layout.simple_dropdown_item_1line, //顯示建議內容的control
                                                   new String[]{"Chinese","English","French","Greek"}); //建議項
//關聯adapter
actv.setAdapter(aa);

MultiAutoCompleteTextView

AutoCompleteTextView匹配整個text view,有時,我們希望能像句子一樣可以每個單詞進行匹配,這種情況需要用到MultiAutoCompleteTextView。

XML例子如下:

<MultiAutoCompleteTextView android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/ui_text_auto_multi"/>

代碼如下。同樣要通過adapter設定建議項,以及顯示建議項的control,並通過setAdapter()與該控制項相關聯。此外,還需要通過Tokenizer告訴系統什麼時候重新進行匹配。

MultiAutoCompleteTextView mt =(MultiAutoCompleteTextView) findViewById(R.id.ui_text_auto_multi);
ArrayAdapter<String> aa = new ArrayAdapter<String>(this,
                                               android.R.layout.simple_dropdown_item_1line,
                                               new String[]{"Chinese","English","French","Greek","Spainish"});
mt.setAdapter(aa);
//通過setTokenizer()告之以什麼作為分割,什麼時候開始進行匹配。通過實現MultiAutoCompleteTextView.Tokenizer介面,提供分割方式。系統已經實現了兩個,一個是CommaTokenizer,以逗號為分割(包括先後有空格),另一個是Rfc822Tokenizer,用於email格式。如要自己實現,可以參考系統實現的兩個例子。
mt.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());

相關連結:
我的Android開發相關文章

聯繫我們

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