Android自动打开和关闭软键盘

现在有一个需求是说希望可以自动的隐藏和打开软键盘,场景是使用在alert dialog中有一个文本框,弹出dialog同时自动弹出键盘,编辑后点击完成直接关闭。使用的方式是:

inputManager.showSoftInput(tvEditor,  InputMethodManager.SHOW_FORCED);

如果使用参数InputMethodManager.SHOW_IMPLICIT,就不会自动弹出键盘,但是使用SHOW_FORCED后使用各种方法都无法自动关闭键盘,必须按back键。

在vivo中很多自动隐藏键盘方法都ok,但是三星没有一个方法可行,包含不仅限于如下方式:

(1)inputManager.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT,InputMethodManager.HIDE_IMPLICIT_ONLY);

(2)dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

(3)dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

(4)inputManager.hideSoftInputFromInputMethod(tvEditor.getWindowToken(),0);

(5)inputManager.hideSoftInputFromInputMethod(dialog.getWindow().getDecorView().getWindowToken(),0);

最后无意中google到,只要toggle成对使用就ok,然后显示使用

inputManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

隐藏也使用

inputManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

目前测试三星和vivo都ok。

时间: 2024-07-29 10:19:12

Android自动打开和关闭软键盘的相关文章

安卓工具类-------->打开或关闭软键盘

import android.content.Context; import android.view.inputmethod.InputMethodManager; import android.widget.EditText; //打开或关闭软键盘 public class KeyBoardUtils { /** * 打卡软键盘 * * @param mEditText输入框 * @param mContext上下文 */ public static void openKeybord(Edi

Win8环境WPF打开和关闭软键盘

代码如下: public class KeyBoardHelper { #region 键盘控制 /// <summary> /// 显示键盘 /// </summary> public static void ShowInputPanel() { string path = @"C:\Program Files\Common Files\microsoft shared\ink\TabTip.exe"; string path32 = @"C:\Pr

android 点击屏幕关闭 软键盘

//点击屏幕 关闭输入弹出框 @Override public boolean onTouchEvent(MotionEvent event) { InputMethodManager im = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); im.hideSoftInputFromWindow(getCurrentFocus().getApplicationWindowToken(), InputMetho

Android开发:在EditText中关闭软键盘 转来的

1.EditText有焦点(focusable为true)阻止输入法弹出 Java代码   editText=(EditText)findViewById(R.id.txtBody); editText.setOnTouchListener(new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { editText.setInputType(InputType.TYPE_NULL); // 关闭软键盘

Android开发:在EditText中关闭软键盘

1.EditText有焦点(focusable为true)阻止输入法弹出 Java代码   editText=(EditText)findViewById(R.id.txtBody); editText.setOnTouchListener(new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { editText.setInputType(InputType.TYPE_NULL); // 关闭软键盘

android 点击关闭软键盘

在项目中,editText获取焦点后,会自动弹出软键盘,关闭的时候一般需要按返回键或者点击软键盘上的按钮, 即使当前activity已经finish掉,软键盘依然存在,会影响用户的体验. 网上目前有很多很详细的办法,比如点击其他空白区域,软键盘就会消失之类的方法,我们项目中没有要求这个,要求的是只要 不遮挡其他操作,还有当前Activity关闭掉后软键盘消失就行, 今天给大家分享两个办法: //此方法,如果显示则隐藏,如果隐藏则显示 private void hintKbOne() { Inpu

Android修炼之道—获取软键盘的高度

自动弹出软键盘 editText = (EditText) findViewById(R.id.edit_text); editText.setFocusable(true); editText.setFocusableInTouchMode(true); editText.requestFocus(); InputMethodManager imm = imm.showSoftInput( edtsearch_title, InputMethodManager.RESULT_SHOWN); i

8、NFC技术:让Android自动打开网页

创建封装Uri的NdefRecord public  NdefRecord  createUri(String  uriString);  public  NdefRecord  createUri(Uri  uri);  范例:自动打开网页 8.NFC技术:让Android自动打开网页

Android不自动弹出软键盘和不让软键盘弹出挤压图形

软键盘弹出挤压图形很变态,设计好的模型会在软件盘弹出数据变得丑陋无比,为了保持不变,只需要在 Manifest.xml 相应的 Activity 里添加 android:windowSoftInputMode="adjustPan|stateHidden" 为了不让软件盘弹出,如果是Activity的话,可以直接添加如下代码解决自动弹出软键盘的问题 <activity android:name="com.guandehao.baobiao.B_KuCunBaoBiao&