How to hide Soft Keyboard when activity starts
I have an Edittext with android:windowSoftInputMode="stateVisible"
in Manifest. Now the keyboard will be shown when I start the activity. How to hide it? I cannot use android:windowSoftInputMode="stateHidden
because when keyboard is visible then minimize the app and resume it the keyboard should be visible. I tried with
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
but it did not work.
使用以下功能显示/隐藏键盘:
/**
* Hides the soft keyboard
*/
public void hideSoftKeyboard() {
if(getCurrentFocus()!=null) {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
}
/**
* Shows the soft keyboard
*/
public void showSoftKeyboard(View view) {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
view.requestFocus();
inputMethodManager.showSoftInput(view, 0);
}
In the AndroidManifest.xml:
<activity android:name="com.your.package.ActivityName"
android:windowSoftInputMode="stateHidden" />
or try
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
Please check this also
只需在editText的父视图中添加两个属性即可。
android:focusable="true"
android:focusableInTouchMode="true"
链接地址: http://www.djcxy.com/p/92996.html
上一篇: 将C#3D数组移植到JS 3D数组
下一篇: 活动开始时如何隐藏软键盘