自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Android EditText不彈出輸入法焦點(diǎn)問題的總結(jié)

移動(dòng)開發(fā) Android
同樣的代碼,碰到有EditText控件的界面時(shí)有的機(jī)子會(huì)彈出輸入法,有的機(jī)子不會(huì)彈出。不好意思,這問題我也一頭霧水,誰知道可以告訴我,否則我就把這個(gè)問題留下來,以后研究android 源碼時(shí)再搞個(gè)清楚。但是...我有解決方案,在這里分享給大家。

看一個(gè)manifest中Activity的配置,如果這個(gè)頁面有EditText,并且我們想要進(jìn)入這個(gè)頁面的時(shí)候默認(rèn)彈出輸入法,可以這樣設(shè)置這個(gè)屬相:android:windowSoftInputMode=stateVisible,這樣就會(huì)默認(rèn)彈起輸入法,當(dāng)然還有別的辦法。

  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 不彈出輸入法總結(jié)

方法一:

在AndroidMainfest.xml中選擇哪個(gè)activity,設(shè)置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失去焦點(diǎn),使用EditText的clearFocus方法

例如:

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

方法三:

強(qiáng)制隱藏Android輸入法窗口

例如:

  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中焦點(diǎn)Focus和彈出輸入法的問題。在網(wǎng)上看了些例子都不夠全面,在這里全面總結(jié)下。

一:EditText為什么會(huì)默認(rèn)彈出輸入法?

同樣的代碼,碰到有EditText控件的界面時(shí)有的機(jī)子會(huì)彈出輸入法,有的機(jī)子不會(huì)彈出。不好意思,這問題我也一頭霧水,誰知道可以告訴我,否則我就把這個(gè)問題留下來,以后研究android 源碼時(shí)再搞個(gè)清楚。但是...我有解決方案。

二:默認(rèn)彈出和默認(rèn)關(guān)閉輸入法的解決方案。

1.默認(rèn)關(guān)閉,不至于進(jìn)入Activity就打開輸入法,影響界面美觀。

①在布局文件中,在EditText前面放置一個(gè)看不到的LinearLayout,讓他率先獲取焦點(diǎn):

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

②方法二:先看一個(gè)屬性android:inputType:指定輸入法的類型,int類型,可以用|選擇多個(gè)。取值可以參考: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改變?yōu)門YPE_NULL,輸入法就不會(huì)彈出.然后再設(shè)置監(jiān)聽,再重新設(shè)置它的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.默認(rèn)彈出。有時(shí)候按照需求可能要求默認(rèn)彈出輸入法。方案如下:

  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.   }  

我覺得因?yàn)樵贏ndroid的主線程中對(duì)UI的操作無效,所以必須在Handler中實(shí)現(xiàn)彈出輸入法的操作。

三:關(guān)于焦點(diǎn)和輸入法的個(gè)人理解

獲取焦點(diǎn)是獲取焦點(diǎn),彈輸入法是彈輸入法。獲取焦點(diǎn)后并不一定會(huì)彈出輸入法,在網(wǎng)上搜了一圈,主流回答是“還有就是已開啟界面就是focus的text的話有可能也是不行的,UI渲染是需要時(shí)間的”......

由于對(duì)源碼不懂,我對(duì)這一點(diǎn)也沒有個(gè)全面的認(rèn)識(shí)。只能將焦點(diǎn)和輸入法分成兩塊來處理。焦點(diǎn)的打開和關(guān)閉特別簡(jiǎn)單。

焦點(diǎn)的獲?。?/p>

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

焦點(diǎn)的取消:

  1. titleInput.setFocusable(false);  

四:關(guān)于經(jīng)常調(diào)用的處理軟鍵盤的函數(shù)如下:

1、打開輸入法窗口:

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

2、關(guān)閉出入法窗口

  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、如果輸入法打開則關(guān)閉,如果沒打開則打開

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

4、獲取輸入法打開的狀態(tài)

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

isOpen若返回true,則表示輸入法打開

責(zé)任編輯:閆佳明 來源: oschina
相關(guān)推薦

2013-06-27 17:26:01

AndroidEditText

2021-08-04 16:36:00

Windows 10Windows微軟

2011-02-22 11:11:33

EditTextAndroid

2013-01-10 11:27:58

Android輸入法分析報(bào)告

2022-01-09 22:59:32

Windows 11Windows微軟

2010-03-05 16:30:29

Android平臺(tái)1.

2010-01-08 16:50:51

Ubuntu SCIM

2010-06-08 18:50:30

OpenSUSE 輸入

2012-02-15 11:14:21

搜狗輸入法

2011-09-15 09:34:48

ubuntu輸入法

2014-01-03 14:59:46

Android

2010-01-11 10:09:14

Ubuntulinux輸入法設(shè)置

2021-10-15 22:17:08

Windows 10Windows微軟

2023-09-09 07:08:37

百度輸入法AI

2009-11-17 10:43:59

ubuntu 9.10輸入法解決方法

2021-04-20 08:30:23

微信微信輸入法張小龍

2021-11-06 23:04:10

Windows 10Windows微軟

2016-12-12 09:58:47

AndroidAndroid Vie

2021-10-09 22:30:49

Windows 10Windows微軟

2013-06-07 15:06:26

搜狗輸入法泄露用戶隱私必應(yīng)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)