我的活动中有三个EditText视图,它们会在某些时候随机隐藏
在EditText中输入单个文本后,软键盘将自动消失
现在我想在EditText获得焦点时显示软键盘
怎么做呢?
提前致谢
我的活动中有三个EditText视图,它们会在某些时候随机隐藏
在EditText中输入单个文本后,软键盘将自动消失
现在我想在EditText获得焦点时显示软键盘
怎么做呢?
提前致谢
通过使用InputMethodManager
,您可以显示和隐藏软键盘。当EditText
获得焦点时,使用toggleSoftInput()
方法显示软键盘,并使用hideSoftInputFromWindow()
在EditText
失去焦点时隐藏键盘,如下所示...
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean isFocused) {
if (isFocused) {
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
} else {
imm.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), 0);
}
}
});
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputMethodManager != null) {
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}