软键盘显示的原理
软件盘的本质是什么?软键盘其实是一个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" //右下角按键内容为'完成'