Android开发中软键盘的常见问题
2017-02-04 10:38
375 查看
软键盘显示的原理
软件盘的本质是什么?软键盘其实是一个Dialog。 InputMethodService为我们的输入法创建了一个Dialog,并且将该Dialog的Window的某些参数(如Gravity)进行了设置,使之能够在底部或者全屏显示。当我们点击输入框时,系统对活动主窗口进行调整,从而为输入法腾出相应的空间,然后将该Dialog显示在底部,或者全屏显示。
自动弹出软键盘
有时候需要一进入Activity后就自动弹出软键盘,可以通过设置一个时间函数来实现,具体写法如下: 方法一:
Timer timer=new Timer(); timer.schedule(new TimerTask() { public void run() { InputMethodManager inputMethodManager=(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); inputMethodManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS); } }, 1000); // 秒后自动弹出1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
方法二:
Timer timer = new Timer(); timer.schedule(new TimerTask() { public void run() { InputMethodManager inputManager = (InputMethodManager) etInput.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); inputManager.showSoftInput(etInput, 0); } }, 1000);// 1秒后自动弹出1
2
3
4
5
6
7
8
9
10
11
1
2
3
4
5
6
7
8
9
10
11
不自动弹出软键盘
有时进入Activity后不希望系统自动弹出软键盘,我们可以按照下面的方法来实现: 方法一:
在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>1
2
3
4
5
6
7
8
9
1
2
3
4
5
6
7
8
9
方法二:
让EditText失去焦点,使用EditText的clearFocus方法
EditText edit=(EditText)findViewById(R.id.edit); edit.clearFocus();1
2
1
2
方法三:
强制隐藏Android输入法窗口
EditText edit=(EditText)findViewById(R.id.edit); InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(edit.getWindowToken(),0);1
2
3
1
2
3
方法四:
EditText始终不弹出软件键盘
EditText edit=(EditText)findViewById(R.id.edit); edit.setInputType(InputType.TYPE_NULL);1
2
1
2
EditText设置ScrollView压缩背景图片解决办法
在你的Activity里加上getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);1
1
动态关闭软键盘
有时希望根据条件动态关闭软键盘,我们可以使用InputMethodManager类,按照下面的方法来实现: 方法一:
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); //得到InputMethodManager的实例 if (imm.isActive()) {//如果开启 imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT,InputMethodManager.HIDE_NOT_ALWAYS);//关闭软键盘,开启方法相同,这个方法是切换开启与关闭状态的 }1
2
3
4
1
2
3
4
方法二:
强制隐藏软键盘 public void KeyBoardCancle() { View view = getWindow().peekDecorView(); if (view != null) { InputMethodManager inputmanger = (InputMethodManager) getSystemService(ActivityBase.INPUT_METHOD_SERVICE); inputmanger.hideSoftInputFromWindow(view.getWindowToken(), 0); } }1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
方法三:
int flags = WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM; getWindow().addFlags(flags);1
2
1
2
方法四:
在onclick事件下.以下方法可行.(如果是EditText失去焦点/得到焦点,没有效果)
InputMethodManager im = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); im.hideSoftInputFromWindow(getCurrentFocus().getApplicationWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);1
2
1
2
InputMethodManager的具体用法可以参考下面的链接:
http://www.apihome.cn/api/android/InputMethodManager.html
软键盘界面按钮功能设置方法
使用android:imeOptinos可对Android自带的软键盘进行一些界面上的设置:<EditText android:id="@+id/text1" android:layout_width="150dip" android:layout_height="wrap_content" android:imeOptions="flagNoExtractUi"/>1
2
3
4
5
1
2
3
4
5
android:imeOptions="flagNoExtractUi" //使软键盘不全屏显示,只占用一部分屏幕1
1
同时,这个属性还能控件软键盘右下角按键的显示内容,默认情况下为回车键
android:imeOptions="actionNone" //输入框右侧不带任何提示 android:imeOptions="actionGo" //右下角按键内容为'开始' android:imeOptions="actionSearch" //右下角按键为放大镜图片,搜索 android:imeOptions="actionSend" //右下角按键内容为'发送' android:imeOptions="actionNext" //右下角按键内容为'下一步' android:imeOptions="actionDone" //右下角按键内容为'完成'
相关文章推荐
- 彻底搞定Android开发中软键盘的常见问题
- 彻底搞定Android开发中软键盘的常见问题
- 彻底搞定Android开发中软键盘的常见问题
- 彻底搞定Android开发中软键盘的常见问题
- 彻底搞定Android开发中软键盘的常见问题
- 彻底搞定Android开发中软键盘的常见问题
- 彻底搞定Android开发中软键盘的常见问题
- Android Edittext 开发常见问题总结(焦点、输入、键盘弹出等)
- Android中软键盘常见的问题
- Android游戏开发9:触屏事件处理中的常见问题及如何禁止横竖屏切换
- Android 开发配置常见问题解决
- Android开发常见问题及解决方法
- Android Launcher开发(五)添加应用程序桌面快捷方常见问题及解决方案
- Android开发常见问题及解决方法小结
- Android开发之ListView+EditText-要命的焦点和软键盘问题解决办法
- Windows下ANDROID应用开发环境安装过程及常见问题
- android google map 开发中的常见问题及解决(无法获取 google map apiKey 和Couldn't get connection factory client)
- Android 开发之系统应用Launcher详解,简单添加和删除快捷方式及常见问题
- Android 开发常见问题
- Android开发环境搭建和常见问题的解决方法