實現(xiàn)Android鍵盤的中英文適配
大家在進行Android項目開發(fā)的時候經(jīng)常會遇到中文輸入和英文輸入切換的情況,本篇內(nèi)容教給大家實現(xiàn)Android下自動識別中英文鍵盤的功能。
英文環(huán)境下,密碼框字體和一般字體不一致問題
1、xml中不能設置inputType 屬性、或者password屬性
2、中文環(huán)境中設置inputType可以
3、當要是適配英文,只能在Java代碼設置
android開發(fā)EditText輸入時彈出數(shù)字輸入鍵盤(適配英文環(huán)境)
首先設置只能輸入數(shù)字
<EditText android:id="@+id/second_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:digits="1234567890" android:maxLength="6" android:paddingLeft="@dimen/dp_20" android:singleLine="true" android:textSize="@dimen/sp_14"/>
重點是
android:digits="1234567890"
EditText中android:digits屬性的作用
是設置允許輸入哪些字符。如“1234567890.+-*/%\n()”
再在代碼里面設置輸入法類型:
secondPassword.setInputType(EditorInfo.TYPE_CLASS_PHONE);//數(shù)字鍵盤 secondPassword.setTransformationMethod(new PasswordTransformationMethod());//密文
則如果該EditText獲得焦點,會彈出數(shù)字輸入法的模擬鍵盤
請在xml中設置inputType屬性即可
1、API中有,列舉出來inputType的值都包括哪些。
android:inputType=”none” android:inputType=”text” android:inputType=”textCapCharacters” 字母大寫 android:inputType=”textCapWords” 首字母大寫 android:inputType=”textCapSentences” 僅第一個字母大寫 android:inputType=”textAutoCorrect” 自動完成 android:inputType=”textAutoComplete” 自動完成 android:inputType=”textMultiLine” 多行輸入 android:inputType=”textImeMultiLine” 輸入法多行(如果支持) android:inputType=”textNoSuggestions” 不提示 android:inputType=”textUri” 網(wǎng)址 android:inputType=”textEmailAddress” 電子郵件地址 android:inputType=”textEmailSubject” 郵件主題 android:inputType=”textShortMessage” 短訊 android:inputType=”textLongMessage” 長信息 android:inputType=”textPersonName” 人名 android:inputType=”textPostalAddress” 地址 android:inputType=”textPassword” 密碼 android:inputType=”textVisiblePassword” 可見密碼 android:inputType=”textWebEditText” 作為網(wǎng)頁表單的文本 android:inputType=”textFilter” 文本篩選過濾 android:inputType=”textPhonetic” 拼音輸入 //數(shù)值類型 android:inputType=”number” 數(shù)字 android:inputType=”numberSigned” 帶符號數(shù)字格式 android:inputType=”numberDecimal” 帶小數(shù)點的浮點格式 android:inputType=”phone” 撥號鍵盤 android:inputType=”datetime” 時間日期 android:inputType=”date” 日期鍵盤 android:inputType=”time” 時間鍵盤
2、Enter鍵圖標的設置
想象一下,當我們在EditText中完成了輸入,想要以輸入的內(nèi)容作為關鍵字進行搜索時,卻需要按下“完成”圖標的Enter按鍵,顯然這不符合良好的用戶體驗設計?!∧敲矗趺礃觼砀淖僂nter按鍵的圖標呢?
Android為我們提供了android:imeOptions來實現(xiàn)這一功能。
android:imeOptions的常用參數(shù)有以下一些:
normal(常規(guī)),
actionUnspecified(未指定),
actionNone(沒有動作),
actionGo(去往),
actionSearch(搜索),
actionSend(發(fā)送),
actionNext(下一個),
actionDone(完成),
flagNoExtractUi,flagNoAccessoryAction,flagNoEnterAction等,其對應的Enter鍵
圖標如圖所示:
3、設置軟鍵盤交互樣式
有時鍵盤彈出需要把界面擠壓到上端或直接覆蓋界面。 可在AndroidManifest.xml 對應的 Activity 里添加上這條屬性:
android:windowSoftInputMode=”參數(shù)”
參數(shù)詳情如下,多個參數(shù)之間可用‘|'隔開:
【A】stateUnspecified:軟鍵盤的狀態(tài)并沒有指定,系統(tǒng)將選擇一個合適的狀態(tài)或依賴于主題的設置
【B】stateUnchanged:當這個activity出現(xiàn)時,軟鍵盤將一直保持在上一個activity里的狀態(tài),無論是隱藏還是顯示
【C】stateHidden:用戶選擇activity時,軟鍵盤總是被隱藏
【D】stateAlwaysHidden:當該Activity主窗口獲取焦點時,軟鍵盤也總是被隱藏的
【E】stateVisible:軟鍵盤通常是可見的
【F】stateAlwaysVisible:用戶選擇activity時,軟鍵盤總是顯示的狀態(tài)
【G】adjustUnspecified:默認設置,通常由系統(tǒng)自行決定是隱藏還是顯示
【H】adjustResize:該Activity總是調(diào)整屏幕的大小以便留出軟鍵盤的空間
【I】adjustPan:當前窗口的內(nèi)容將自動移動以便當前焦點從不被鍵盤覆蓋和用戶能總是看到輸入內(nèi)容的部分
EditText默認不彈出軟件鍵盤:
方法一:
在 AndroidMainfest.xml 中選擇哪個 activity,設置windowSoftInputMode 屬性為 adjustUnspecified|stateHidden
例如:
<activity android:name=".Main" android:label="@string/app_name" android:windowSoftInputMode="adjustUnspecified|stateHidden" android:configChanges="orientation|keyboardHidden"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
方法二:
讓EditText失去焦點,使用EditText的clearFocus方法
例如:
EditText edit=(EditText)findViewById(R.id.edit); edit.clearFocus();
方法三:
強制隱藏Android輸入法窗口
例如:
EditText edit=(EditText)findViewById(R.id.edit); InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(edit.getWindowToken(),0);
4、自動將輸入的小寫字母轉(zhuǎn)換為大寫
自動轉(zhuǎn)化為大寫字母。但是轉(zhuǎn)換出來的只是顯示為大寫字母,存的還是小寫字母。
class InputLowerToUpper extends ReplacementTransformationMethod{ @Override protected char[] getOriginal() { char[] lower = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z' }; return lower; } @Override protected char[] getReplacement() { char[] upper = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z' }; return upper; } } editText.setTransformationMethod(new InputLowerToUpper());
也可通過設置 android:inputType=”textCapCharacters”可行,但是就不能一塊使用密碼鍵盤了。
//下面這種方法才是真正的將輸入的小寫字母轉(zhuǎn)換為大寫字母 addressText.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub addressText.removeTextChangedListener(this);//解除文字改變事件 addressText.setText(s.toString().toUpperCase());//轉(zhuǎn)換 addressText.setSelection(s.toString().length());//重新設置光標位置 addressText.addTextChangedListener(this);//重新綁 // licensePlateNumber = addressText.getText().toString().trim(); } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { // TODO Auto-generated method stub } @Override public void afterTextChanged(Editable arg0) { // TODO Auto-generated method stub } });
- Android開發(fā)中軟鍵盤的顯示和隱藏
- 詳解Android中獲取軟鍵盤狀態(tài)和軟鍵盤高度
- Android屏蔽軟鍵盤并且顯示光標的實例詳解
- Android開發(fā)之如何自定義數(shù)字鍵盤詳解
- Android 監(jiān)聽軟鍵盤狀態(tài)的實例詳解
- Android 軟鍵盤狀態(tài)并隱藏輸入法的實例
- Android 開發(fā)之Dialog中隱藏鍵盤的正確使用方法
- Android 表情面板和軟鍵盤切換時跳閃問題的解決方法
- Android View 完美實現(xiàn)EditText 在軟鍵盤上邊的示例
- Android編程之ListView和EditText發(fā)布帖子隱藏軟鍵盤功能詳解
- Android自定義鍵盤的實現(xiàn)(數(shù)字鍵盤和字母鍵盤)
相關文章
Android開發(fā)之獲取LayoutInflater對象的方法總結(jié)
這篇文章主要介紹了Android開發(fā)之獲取LayoutInflater對象的方法,結(jié)合實例形式總結(jié)分析了Android獲取LayoutInflater對象的常用技巧,需要的朋友可以參考下2016-02-02詳解Android 全局彈出對話框SYSTEM_ALERT_WINDOW權(quán)限
本篇文章主要介紹了詳解Android 全局彈出對話框SYSTEM_ALERT_WINDOW權(quán)限,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03Android dip,px,pt,sp 的區(qū)別詳解
本篇文章是對Android中dip,px,pt,sp的區(qū)別進行了詳細的分析介紹,需要的朋友參考下2013-06-06Android性能優(yōu)化之圖片大小,尺寸壓縮綜合解決方案
隨著Android手機的越來越先進,給我們開發(fā)者而言傳遞的圖片也是越來越大,這個時候我們可以對一些沒有必要原圖展示的圖片進行壓縮,這篇文章主要給大家介紹了關于Android性能優(yōu)化之圖片大小,尺寸壓縮的綜合解決方案,需要的朋友可以參考下2022-04-04android使用Ultra-PullToRefresh實現(xiàn)下拉刷新自定義代碼
本篇文章主要介紹了android使用Ultra-PullToRefresh實現(xiàn)下拉刷新新自定義,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-02-02Android ListView和Adapter數(shù)據(jù)適配器的簡單介紹
這篇文章主要介紹了Android ListView和Adapter數(shù)據(jù)適配器的簡單介紹,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04