android EditText 軟鍵盤 一些總結

來源:互聯網
上載者:User

標籤:android   des   http   使用   strong   os   

看一個manifest中Activity的配置,如果這個頁面有EditText,並且我們想要進入這個頁面的時候預設彈出IME,可以這樣設定這個屬相:android:windowSoftInputMode=stateVisible,這樣就會預設彈起IME,當然還有別的辦法。

  1. <activity android:name=".ui.login" 
  2.                   android:configChanges="orientation|keyboardHidden|locale" 
  3.                   android:screenOrientation="portrait" 
  4.                   android:windowSoftInputMode="stateVisible|adjustPan" > 
  5.         </activity>  

Android EditText 不彈出IME總結

方法一:

在AndroidMainfest.xml中選擇哪個activity,設定windowSoftInputMode屬性為adjustUnspecified|stateHidden

例如:

  1. <activity android:name=".Main" 
  2. android:label="@string/app_name" 
  3. android:windowSoftInputMode="adjustUnspecified|stateHidden" 
  4. android:configChanges="orientation|keyboardHidden"> 
  5. < intent-filter> 
  6. < action android:name="android.intent.action.MAIN" /> 
  7. < category android:name="android.intent.category.LAUNCHER" /> 
  8. < /intent-filter> 
  9. < /activity>  

方法二:

讓EditText失去焦點,使用EditText的clearFocus方法

例如:

  1. EditText edit=(EditText)findViewById(R.id.edit); 
  2. edit.clearFocus();  

方法三:

強制隱藏AndroidIME視窗

例如:

  1. EditText edit=(EditText)findViewById(R.id.edit); 
  2. InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
  3. imm.hideSoftInputFromWindow(edit.getWindowToken(),0);  

2.EditText始終不彈出軟體機碼盤

例:

  1. EditText edit=(EditText)findViewById(R.id.edit); 
  2. edit.setInputType(InputType.TYPE_NULL);  

研究了下android中焦點Focus和彈出IME的問題。在網上看了些例子都不夠全面,在這裡全面總結下。

一:EditText為什麼會預設彈出IME?

同樣的代碼,碰到有EditText控制項的介面時有的機子會彈出IME,有的機子不會彈出。不好意思,這問題我也一頭霧水,誰知道可以告訴我,否則我就把這個問題留下來,以後研究android 源碼時再搞個清楚。但是...我有解決方案。

二:預設彈出和預設關閉IME的解決方案。

1.預設關閉,不至於進入Activity就開啟IME,影響介面美觀。

①在布局檔案中,在EditText前面放置一個看不到的LinearLayout,讓他率先擷取焦點:

  1. <LinearLayout 
  2. android:focusable="true" 
  3. android:focusableInTouchMode="true" 
  4. android:layout_width="0px" 
  5. android:layout_height="0px"/>  

②方法二:先看一個屬性android:inputType:指定IME的類型,int類型,可以用|選擇多個。取值可以參考:android.text.InputType類。取值包括:text,textUri,

phone,number,等.

Android SDK中有這麼一句話“If the given content type is TYPE_NULL then a soft keyboard will not be displayed for this text view”,

先將EditText的InputType改變為TYPE_NULL,IME就不會彈出.然後再設定監聽,再重新設定它的InputType.

  1. editText.setOnTouchListener(new OnTouchListener() { 
  2.                 public boolean onTouch(View v, MotionEvent event) {  
  3.                     // TODO Auto-generated method stub  
  4.                     int inType = editText.getInputType(); // backup the input type  
  5.                     editText.setInputType(InputType.TYPE_NULL); // disable soft input      
  6.                     editText.onTouchEvent(event); // call native handler      
  7.                     editText.setInputType(inType); // restore input type     
  8.                     return true;                      
  9.                 }  
  10.             });  

2.預設彈出。有時候按照需求可能要求預設彈出IME。方案如下:

  1. EditText titleInput = (EditText) findViewById(R.id.create_edit_title); 
  2. titleInput.setFocusable(true); 
  3.  titleInput.requestFocus(); 
  4.  onFocusChange(titleInput.isFocused()); 
  5. private void onFocusChange(boolean hasFocus) 
  6. { 
  7. final boolean isFocus = hasFocus; 
  8. (new Handler()).postDelayed(new Runnable() { 
  9. public void run() { 
  10. InputMethodManager imm = (InputMethodManager) 
  11. titleInput.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); 
  12. if(isFocus) 
  13. { 
  14. imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS); 
  15. } 
  16. else 
  17. { 
  18. imm.hideSoftInputFromWindow(titleInput.getWindowToken(),0); 
  19. } 
  20. } 
  21. }, 100); 
  22. }  

我覺得因為在Android的主線程中對UI的操作無效,所以必須在Handler中實現彈出IME的操作。

三:關於焦點和IME的個人理解

擷取焦點是擷取焦點,彈IME是彈IME。擷取焦點後並不一定會彈出IME,在網上搜了一圈,主流回答是“還有就是已開啟介面就是focus的text的話有可能也是不行的,UI渲染是需要時間的”......

由於對源碼不懂,我對這一點也沒有個全面的認識。只能將焦點和IME分成兩塊來處理。焦點的開啟和關閉特別簡單。

焦點的擷取:

  1. titleInput.setFocusable(true); 
  2. titleInput.requestFocus();  

焦點的取消:

  1. titleInput.setFocusable(false);  

四:關於經常調用的處理軟鍵盤的函數如下:

1、開啟IME視窗:

  1. InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
  2. // 接受軟鍵盤輸入的編輯文本或其它視圖 
  3. imm.showSoftInput(submitBt,InputMethodManager.SHOW_FORCED);  

2、關閉出入法視窗

  1. InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
  2. inputMethodManager.hideSoftInputFromWindow(OpeListActivity.this.getCurrentFocus().getWindowToken(), 
  3. InputMethodManager.HIDE_NOT_ALWAYS); 
  4. //接受軟鍵盤輸入的編輯文本或其它視圖 
  5. inputMethodManagerwww.2cto.com 
  6. .showSoftInput(submitBt,InputMethodManager.SHOW_FORCED);  

3、如果IME開啟則關閉,如果沒開啟則開啟

  1. InputMethodManager m=(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
  2. m.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);  

4、擷取IME開啟的狀態

  1. InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
  2. boolean isOpen=imm.isActive();  

isOpen若返回true,則表示IME開啟

 

原文地址:http://mobile.51cto.com/aprogram-403138.htm

聯繫我們

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