软键盘显示的原理
软件盘的本质是什么?软键盘其实是一个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);
方法二:
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);
不自动弹出软键盘
有时进入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>
方法二:
让EditText失去焦点,使用EditText的clearFocus方法
EditText edit=(EditText)findViewById(R.id.edit);
edit.clearFocus();
方法三:
强制隐藏Android输入法窗口
EditText edit=(EditText)findViewById(R.id.edit)
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)
imm.hideSoftInputFromWindow(edit.getWindowToken(),0)
方法四:
EditText始终不弹出软件键盘
EditText edit=(EditText)findViewById(R.id.edit)
edit.setInputType(InputType.TYPE_NULL)
EditText设置ScrollView压缩背景图片解决办法
在你的Activity里加上
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)
动态关闭软键盘
有时希望根据条件动态关闭软键盘,我们可以使用InputMethodManager类,按照下面的方法来实现:
方法一:
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)
if (imm.isActive()) {//如果开启
imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT,InputMethodManager.HIDE_NOT_ALWAYS)
}
方法二:
强制隐藏软键盘
public void KeyBoardCancle() {
View view = getWindow().peekDecorView();
if (view != null) {
InputMethodManager inputmanger = (InputMethodManager) getSystemService(ActivityBase.INPUT_METHOD_SERVICE);
inputmanger.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
方法三:
int flags = WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM
getWindow().addFlags(flags)
方法四:
在onclick事件下.以下方法可行.(如果是EditText失去焦点/得到焦点,没有效果)
InputMethodManager im = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)
im.hideSoftInputFromWindow(getCurrentFocus().getApplicationWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS)
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"/>
android:imeOptions="flagNoExtractUi"
同时,这个属性还能控件软键盘右下角按键的显示内容,默认情况下为回车键
android:imeOptions="actionNone"
android:imeOptions="actionGo"
android:imeOptions="actionSearch"
android:imeOptions="actionSend"
android:imeOptions="actionNext"
android:imeOptions="actionDone"