編寫:zhaochunqi - 原文:每個文字框都對應特定型別的文字輸入,如email位址,**號碼,或者純文字。為應用中的每乙個文字框指定輸入型別是很重要的,這樣做可以讓系統展示更為合適的軟輸入法(比如虛擬鍵盤)。
除了輸入法可用的按鈕型別之外,我們還應該指定一些行為,例如,輸入法是否提供拼寫建議,新的句子首字母大寫,和將回車按鈕替換成動作按鈕(如 done 或者 next)。這節課介紹了如何新增這些屬性。
通過將 android:inputtype 屬性新增到 節點中,我們可以為文字框宣告輸入法。
舉例來說,如果我們想要乙個用於輸入**號碼的輸入法,那麼使用"phone"
值:
figure 1.
android:id="@+id/phone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/phone_hint"
android:inputtype="phone" />
phone
輸入型別
或者如果文字框用於輸入密碼,那麼使用"textpassword"
值來隱藏使用者的輸入:
figure 2.
android:id="@+id/password"
android:hint="@string/password_hint"
android:inputtype="textpassword"
... />
textpassword
輸入型別
有幾種可供選擇的值在android:inputtype
記錄在屬性中,一些值可以組合起來實現特定的輸入法外觀和附加的行為。
android:inputtype 屬性允許我們為輸入法指定不同的行為。最為重要的是,如果文字框用於基本的文字輸入(如簡訊息),那麼我們應該使用"textautocorrect"
值來開啟自動拼寫修正。
figure 3. 新增textautocorrect
為拼寫錯誤提供自動修正
我們可以將不同的行為和輸入法形式組合到 android:inputtype 這個屬性。如:如何建立乙個文字框,裡面的句子首字母大寫並開啟拼寫修正:
多數的軟鍵盤會在底部角落裡為使用者提供乙個合適的動作按鈕來觸發當前文字框的操作。預設情況下,系統使用 next 或者 done,除非我們的文字框允許多行文字(如
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputtype=
"textcapsentences|textautocorrect"
... />
android:inputtype="textmultiline"
),這種情況下,動作按鈕就是回車換行。然而,我們可以指定一些更適合我們文字框的額外動作,比如 send 和 go。
figure 4. 當我們宣告了android:imeoptions="actionsend"
,會出現 send 按鈕。
使用android:imeoptions 屬性,並設定乙個動作值(如"actionsend"
或"actionsearch"
),來指定鍵盤的動作按鈕。如:
然後,我們可以通過為 edittext 節點定義 textview.oneditoractionlistener 來監聽動作按鈕的按壓。在***中,響應 editorinfo 類中定義的適合的 ime action id,如 ime_action_send 。例如:
android:id="@+id/search"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/search_hint"
android:inputtype="text"
android:imeoptions="actionsend" />
edittext edittext = (edittext) findviewbyid(r.id.search);
edittext.setoneditoractionlistener(new oneditoractionlistener()
return handled;
}
});
**自:
Android輸入法開發
android 1.5 新特色之一就是輸入法框架 input method framework,imf 正是它的出現,才為誕生不帶實體鍵盤的裝置提供了可能。imf設計用來支援不同的ime,包括了soft keyboard,hand writing recognizes和hard keyboard t...
Android輸入法教程
本帖最後由 yanghe123 於 2012 4 28 15 51 編輯 android輸入法教程 輸入法框架 android輸入法框梳理 在android中建立一種新的輸入法 android 呼叫輸入法 android輸入法手勢程式原始碼 android輸入法開發例項 android 輸入法的問題...
Android 隱藏輸入法
對於edittext 可以設定setinputtype inputtype.type null 輸入法就不會彈出。需要的時候在動態設定inputtyep.如果一載入activity 可以用getwindow setsoftinputmode windowmanager.layoutparams.so...