如何在使用 InputMethodManager 隐藏后恢复软键盘
how to bring back soft keyboard after hiding using InputMethodManager
我是 Android 编程新手,所以只是制作一个简单的应用程序。
在应用程序中,我有一个 EditText 组件,它会在键盘弹出时向上滑动,我不希望它向上滑动,所以我搜索了它并解决了使用问题,
在 OnCreate 方法中
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
但问题是在使用此行后,EditText 在单击输入键盘后就位,但键盘没有出现 away.So,我搜索它并找到了隐藏键盘的方法
public static void hideSoftKeyboard(Activity activity) {
InputMethodManager inputMethodManager =
(InputMethodManager) activity.getSystemService(
Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(
activity.getCurrentFocus().getWindowToken(), 0);
但是现在的问题是键盘隐藏成功了,但是在我重新点击EditText.So之后不可见,我在网上搜索但是没有运气,开始在Android中搜索方法用于使键盘可见并弄清楚这是什么的 Studio
public static void showSoftKeyboard(Activity activity){
InputMethodManager inputMethodManager1 =
(InputMethodManager) activity.getSystemService(
Activity.INPUT_METHOD_SERVICE);
inputMethodManager1.showSoftInputFromInputMethod(
activity.getCurrentFocus().getWindowToken(), 0);
但它不起作用,抛出 NullPointerException。
所以请任何人帮我解决这个问题。
还有没有其他方法可以让 EditText 保持在其位置上而不向上滑动。这样就不需要应用这种隐藏和显示方法,如果没有,请告诉我如何取回键盘。
评论你的逻辑,请尝试以下方法,
在 Android Manifest
中的 activity 标签中写下一行,如下所示
<activity android:name=".yourActivityName"
android:windowSoftInputMode="adjustPan">
</activity>
然后将此行添加到您的 edittext
xml 中 layout
android:imeOptions="actionDone"
喜欢下面
<EditText
android:id="@+id/edt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Edittext"
android:singleLine="true"
android:maxLines="1"
android:imeOptions="actionDone"/>
或通过您的代码进行设置
yourEditText.setImeOptions(EditorInfo.IME_ACTION_DONE);
点击软键盘上的完成按钮,它会自动关闭。
下面是软键盘完成click
事件的代码button
。
yourEditText.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId==EditorInfo.IME_ACTION_DONE){
// write your code here
}
return false;
}
});
试试这个
public void showSoftKeyboard(Context context, View view){
if(view.requestFocus()){
InputMethodManager imm =(InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
}
}
或
public void showSoftKeyboard(Context context){
if(context == null) return;
InputMethodManager imm =(InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
}
我是 Android 编程新手,所以只是制作一个简单的应用程序。
在应用程序中,我有一个 EditText 组件,它会在键盘弹出时向上滑动,我不希望它向上滑动,所以我搜索了它并解决了使用问题,
在 OnCreate 方法中
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
但问题是在使用此行后,EditText 在单击输入键盘后就位,但键盘没有出现 away.So,我搜索它并找到了隐藏键盘的方法
public static void hideSoftKeyboard(Activity activity) {
InputMethodManager inputMethodManager =
(InputMethodManager) activity.getSystemService(
Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(
activity.getCurrentFocus().getWindowToken(), 0);
但是现在的问题是键盘隐藏成功了,但是在我重新点击EditText.So之后不可见,我在网上搜索但是没有运气,开始在Android中搜索方法用于使键盘可见并弄清楚这是什么的 Studio
public static void showSoftKeyboard(Activity activity){
InputMethodManager inputMethodManager1 =
(InputMethodManager) activity.getSystemService(
Activity.INPUT_METHOD_SERVICE);
inputMethodManager1.showSoftInputFromInputMethod(
activity.getCurrentFocus().getWindowToken(), 0);
但它不起作用,抛出 NullPointerException。 所以请任何人帮我解决这个问题。
还有没有其他方法可以让 EditText 保持在其位置上而不向上滑动。这样就不需要应用这种隐藏和显示方法,如果没有,请告诉我如何取回键盘。
评论你的逻辑,请尝试以下方法,
在 Android Manifest
中的 activity 标签中写下一行,如下所示
<activity android:name=".yourActivityName"
android:windowSoftInputMode="adjustPan">
</activity>
然后将此行添加到您的 edittext
xml 中 layout
android:imeOptions="actionDone"
喜欢下面
<EditText
android:id="@+id/edt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Edittext"
android:singleLine="true"
android:maxLines="1"
android:imeOptions="actionDone"/>
或通过您的代码进行设置
yourEditText.setImeOptions(EditorInfo.IME_ACTION_DONE);
点击软键盘上的完成按钮,它会自动关闭。
下面是软键盘完成click
事件的代码button
。
yourEditText.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId==EditorInfo.IME_ACTION_DONE){
// write your code here
}
return false;
}
});
试试这个
public void showSoftKeyboard(Context context, View view){
if(view.requestFocus()){
InputMethodManager imm =(InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
}
}
或
public void showSoftKeyboard(Context context){
if(context == null) return;
InputMethodManager imm =(InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
}