我想在EditText被聚焦时自动显示软键盘(如果设备没有物理键盘),我有两个问题:
当我的活动显示时,我的EditText是集中的,但键盘不显示,我需要再次单击它来显示键盘(它应该显示时,我的活动显示)。 当我在键盘上点击完成时,键盘被解散,但EditText保持集中,我不想(因为我的编辑已经完成)。
继续说,我的问题是有一些更像iPhone的东西:保持键盘与我的EditText状态同步(聚焦/不聚焦),当然,如果有物理键盘,也不会呈现软键盘。
我想在EditText被聚焦时自动显示软键盘(如果设备没有物理键盘),我有两个问题:
当我的活动显示时,我的EditText是集中的,但键盘不显示,我需要再次单击它来显示键盘(它应该显示时,我的活动显示)。 当我在键盘上点击完成时,键盘被解散,但EditText保持集中,我不想(因为我的编辑已经完成)。
继续说,我的问题是有一些更像iPhone的东西:保持键盘与我的EditText状态同步(聚焦/不聚焦),当然,如果有物理键盘,也不会呈现软键盘。
当前回答
当其他方法都不起作用时,就强制显示它:
editText.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
然后,如果你想关闭它,例如在onPause()中,你可以调用:
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
其他回答
Kotlin扩展显示键盘的焦点。
这是之前的回答的组合,要么太长,要么不完整。
这个扩展在消息队列上发布了一个可运行的消息,在请求聚焦后显示软键盘:
fun View.showSoftKeyboard() {
post {
if (this.requestFocus()) {
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm?.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT)
}
}
}
在需要时从任何视图调用它:
editText.showSoftKeyboard()
我做了这个帮助课。 只需要传递context和你想要聚焦的View show keyboard和hide keyboard。 我希望这能有所帮助。
public class FocusKeyboardHelper {
private View view;
private Context context;
private InputMethodManager imm;
public FocusKeyboardHelper(Context context, View view){
this.view = view;
this.context = context;
imm = (InputMethodManager) context.getSystemService(context.INPUT_METHOD_SERVICE);
}
public void focusAndShowKeyboard(){
view.requestFocus();
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
}
public void hideKeyBoard(){
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
根据这个答案,我使用了setSoftInputMode方法,并覆盖了DialogFragment中的这些方法:
@Override
public void onCancel(@NonNull DialogInterface dialog) {
super.onCancel(dialog);
requireDialog().getWindow().setSoftInputMode(InputMethodManager.HIDE_IMPLICIT_ONLY);
}
@Override
public void onStart() {
super.onStart();
requireDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
@Override
public void onStop() {
super.onStop();
requireDialog().getWindow().setSoftInputMode(InputMethodManager.HIDE_IMPLICIT_ONLY);
}
我也用这些方法创建了我自己的DialogFragment子类,所以当你创建另一个对话框并从这个类继承时,你会自动显示软键盘,而不需要任何其他编辑。希望对别人有用。
信不信由你,当我发现活动动画可以禁用软键盘时,我的软键盘问题得到了解决。当你用
i.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
and
overridePendingTransition(0, 0);
它可以隐藏软键盘,没有办法显示它。
我用定时器。键盘可以显示编辑文本聚焦。
edittext = (EditText) findViewById(R.id.edittext );
edittext.requestFocus();
edittext.setFocusableInTouchMode(true);
if (edittext.requestFocus()) {
final Thread timer = new Thread() {
public void run() {
try{
sleep(500);
InputMethodManager imm =(InputMethodManager) getApplicationContext().getSystemService(INPUT_METHOD_SERVICE);
imm.showSoftInput(edittext, SHOW_IMPLICIT);
} catch (Exception e) {
e.printStackTrace();
}
}
};
timer.start();