我的布局中有一个EditText和一个Button。
在编辑字段中写入并单击按钮后,我想在触摸键盘外部时隐藏虚拟键盘。我假设这是一段简单的代码,但我在哪里可以找到它的示例?
我的布局中有一个EditText和一个Button。
在编辑字段中写入并单击按钮后,我想在触摸键盘外部时隐藏虚拟键盘。我假设这是一段简单的代码,但我在哪里可以找到它的示例?
当前回答
从碎片移动到碎片时
fun hideKeyboard(activity: Activity?): Boolean {
val inputManager = activity?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager?
if (inputManager != null) {
val currentFocus = activity?.currentFocus
if (currentFocus != null) {
val windowToken = currentFocus.windowToken
if (windowToken != null) {
return inputManager.hideSoftInputFromWindow(windowToken, 0)
}
}
}
return false
}
fun showKeyboard(editText: EditText) {
val imm = editText.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(editText.windowToken, 0)
imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0)
editText.requestFocus()
}
其他回答
每次都像魔术般
private void closeKeyboard() {
InputMethodManager inputManager = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
private void openKeyboard() {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
if(imm != null){
imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
}
}
我创建了一个布局,部分来自xml,部分来自自定义布局引擎,所有这些都在代码中处理。对我来说,唯一有效的方法是跟踪键盘是否打开,并使用如下键盘切换方法:
public class MyActivity extends Activity
{
/** This maintains true if the keyboard is open. Otherwise, it is false. */
private boolean isKeyboardOpen = false;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
LayoutInflater inflater;
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View contentView = inflater.inflate(context.getResources().getIdentifier("main", "layout", getPackageName()), null);
setContentView(contentView);
contentView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener()
{
public void onGlobalLayout()
{
Rect r = new Rect();
contentView.getWindowVisibleDisplayFrame(r);
int heightDiff = contentView.getRootView().getHeight() - (r.bottom - r.top);
if (heightDiff > 100)
isKeyboardVisible = true;
else
isKeyboardVisible = false;
});
}
}
public void closeKeyboardIfOpen()
{
InputMethodManager imm;
imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (isKeyboardVisible)
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
}
}
下面是隐藏和显示方法。
科特林
fun hideKeyboard(activity: Activity) {
val v = activity.currentFocus
val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
assert(v != null)
imm.hideSoftInputFromWindow(v!!.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
}
private fun showKeyboard(activity: Activity) {
val v = activity.currentFocus
val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
assert(v != null)
imm.showSoftInput(v, InputMethodManager.SHOW_IMPLICIT)
}
Java
public static void hideKeyboard(Activity activity) {
View v = activity.getCurrentFocus();
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
assert imm != null && v != null;
imm.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
private static void showKeyboard(Activity activity) {
View v = activity.getCurrentFocus();
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
assert imm != null && v != null;
imm.showSoftInput(v, InputMethodManager.SHOW_IMPLICIT);
}
简单编码:在onCreate()中使用此代码
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN
);
对于那些寻找喷气背包的人来说,写下答案。
val keyboardController = LocalSoftwareKeyboardController.current
//for showing the keyboard
keyboardController?.show()
//for hiding the keyboard
keyboardController?.hide()