【转】EditText不弹出输入法,焦点问题的总结

来源:互联网 发布:淘宝店铺的权重 编辑:程序博客网 时间:2024/04/30 14:48

转自:http://mp.weixin.qq.com/s?__biz=MzAwOTUyNzI3Ng==&mid=2652071079&idx=1&sn=3ea3afd2909c21acc9e0199fbb3156b6&scene=0#wechat_redirect

今天偶然浏览到一个关于EditText弹出输入法和焦点的问题,就总结一下跟大家进行分享。

首先呢获取焦点是获取焦点,弹输入法是弹输入法,获取焦点后并不一定会弹出输入法。

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

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

Android EditText 不弹出输入法总结

1、在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> 

2、让EditText失去焦点,使用EditText的clearFocus方法 ,例如

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

3、强制隐藏Android输入法窗口,例如

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

4、EditText始终不弹出软件键盘,例如

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

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

2.1通常为了不影响美观,我们给输入法设置默认关闭

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

<LinearLayout    android:focusable="true"    android:focusableInTouchMode="true"    android:layout_width="0px"    android:layout_height="0px"/>

2.1.2先看一个属性android:inputType:指定输入法的类型,int类型,可以用|选择多个。取值可以参考:android.text.InputType类。取值包括:text,textUri,phone,number,等.先将EditText的InputType改变为TYPE_NULL,输入法就不会弹出.然后再设置监听,再重新设置它的InputType

editText.setOnTouchListener(new OnTouchListener() {    public boolean onTouch(View v, MotionEvent event) {          int inType = editText.getInputType();        editText.setInputType(InputType.TYPE_NULL);              editText.onTouchEvent(event);          editText.setInputType(inType);           return true;                          }  });

2.2.默认弹出。有时候按照需求可能要求默认弹出输入法。方案如下

EditText titleInput = (EditText) findViewById(R.id.create_edit_title);titleInput.setFocusable(true);titleInput.requestFocus();onFocusChange(titleInput.isFocused());private void onFocusChange ( boolean hasFocus){    final boolean isFocus = hasFocus;    (new Handler()).postDelayed(new Runnable() {        public void run() {            InputMethodManager imm =                    (InputMethodManager) titleInput.getContext()                            .getSystemService(Context.INPUT_METHOD_SERVICE);            if (isFocus) {                imm.toggleSoftInput(0,                        InputMethodManager.HIDE_NOT_ALWAYS);            } else {                imm.hideSoftInputFromWindow(                        titleInput.getWindowToken(), 0);            }        }    }, 100);}

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

1、打开输入法窗口

InputMethodManager inputMethodManager =                (InputMethodManager)getSystemService(                    Context.INPUT_METHOD_SERVICE);// 接受软键盘输入的编辑文本或其它视图imm.showSoftInput(submitBt, InputMethodManager.SHOW_FORCED);

2、关闭输入法窗口

InputMethodManager inputMethodManager =        (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);inputMethodManager.hideSoftInputFromWindow(        OpeListActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);//接受软键盘输入的编辑文本或其它inputMethodManager.showSoftInput(submitBt, InputMethodManager.SHOW_FORCED);

3、如果输入法打开则关闭,如果没打开则打开

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

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

InputMethodManager imm = (InputMethodManager)                getSystemService(Context.INPUT_METHOD_SERVICE);boolean isOpen=imm.isActive();//isOpen若返回true,则表示输入法打开



0 0
原创粉丝点击