Android EditText不弹出输入法焦点问题的总结

移动开发 Android
同样的代码,碰到有EditText控件的界面时有的机子会弹出输入法,有的机子不会弹出。不好意思,这问题我也一头雾水,谁知道可以告诉我,否则我就把这个问题留下来,以后研究android 源码时再搞个清楚。但是...我有解决方案,在这里分享给大家。

看一个manifest中Activity的配置,如果这个页面有EditText,并且我们想要进入这个页面的时候默认弹出输入法,可以这样设置这个属相:android:windowSoftInputMode=stateVisible,这样就会默认弹起输入法,当然还有别的办法。

  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 不弹出输入法总结

方法一:

在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();  

方法三:

强制隐藏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中焦点Focus和弹出输入法的问题。在网上看了些例子都不够全面,在这里全面总结下。

一:EditText为什么会默认弹出输入法?

同样的代码,碰到有EditText控件的界面时有的机子会弹出输入法,有的机子不会弹出。不好意思,这问题我也一头雾水,谁知道可以告诉我,否则我就把这个问题留下来,以后研究android 源码时再搞个清楚。但是...我有解决方案。

二:默认弹出和默认关闭输入法的解决方案。

1.默认关闭,不至于进入Activity就打开输入法,影响界面美观。

①在布局文件中,在EditText前面放置一个看不到的LinearLayout,让他率先获取焦点:

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

②方法二:先看一个属性android:inputType:指定输入法的类型,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,输入法就不会弹出.然后再设置监听,再重新设置它的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.默认弹出。有时候按照需求可能要求默认弹出输入法。方案如下:

  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中实现弹出输入法的操作。

三:关于焦点和输入法的个人理解

获取焦点是获取焦点,弹输入法是弹输入法。获取焦点后并不一定会弹出输入法,在网上搜了一圈,主流回答是“还有就是已开启界面就是focus的text的话有可能也是不行的,UI渲染是需要时间的”......

由于对源码不懂,我对这一点也没有个全面的认识。只能将焦点和输入法分成两块来处理。焦点的打开和关闭特别简单。

焦点的获取:

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

焦点的取消:

  1. titleInput.setFocusable(false);  

四:关于经常调用的处理软键盘的函数如下:

1、打开输入法窗口:

  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、如果输入法打开则关闭,如果没打开则打开

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

4、获取输入法打开的状态

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

isOpen若返回true,则表示输入法打开

责任编辑:闫佳明 来源: oschina
相关推荐

2013-06-27 17:26:01

AndroidEditText

2011-02-22 11:11:33

EditTextAndroid

2021-08-04 16:36:00

Windows 10Windows微软

2013-01-10 11:27:58

Android输入法分析报告

2022-01-09 22:59:32

Windows 11Windows微软

2010-03-05 16:30:29

Android平台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

2009-11-17 10:43:59

ubuntu 9.10输入法解决方法

2010-01-11 10:09:14

Ubuntulinux输入法设置

2023-09-09 07:08:37

百度输入法AI

2021-10-15 22:17:08

Windows 10Windows微软

2021-04-20 08:30:23

微信微信输入法张小龙

2016-12-12 09:58:47

AndroidAndroid Vie

2021-11-06 23:04:10

Windows 10Windows微软

2021-10-09 22:30:49

Windows 10Windows微软

2013-06-07 15:06:26

搜狗输入法泄露用户隐私必应
点赞
收藏

51CTO技术栈公众号