简体   繁体   English

当AlertText对话框中的EditText处于焦点时,Android键盘不显示

[英]Android keyboard not showing when EditText is in focus, inside Alert Dialog

Lots of people seem to have had this problem, but none of their solutions are helping me. 很多人似乎都遇到了这个问题,但是他们的解决方案都没有帮助我。

Basically I create a custom alert dialog, and set a linearlayout as it's view. 基本上,我创建一个自定义警报对话框,并在其视图中设置一个线性布局。

If I add an editText to it before showing the dialog, everything works fine and the keyboard show up. 如果在显示对话框之前在其中添加了editText,则一切正常,并显示键盘。

However, if I add editText to the linear layout while the dialog is up, the keyboard doesn't show up even if I click on the edit text and even if it didn't have focus previously. 但是,如果在对话框打开时将editText添加到线性布局中,则即使我单击编辑文本并且以前没有焦点,键盘也不会显示。

I tried 我试过了

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

however no keyboard shows up ever. 但是从未出现过键盘。

I also tried: 我也尝试过:

InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if(imm!=null)
{
    imm.toggleSoftInput(0, InputMethodManager.SHOW_IMPLICIT);
}

from inside the alertdialog, however the keyboard that shows up, is behind the dialog and is therefore unusable. 从alertdialog内部,但是显示的键盘位于对话框的后面,因此无法使用。

I just don't understand what the problem is, do any of you know a solution? 我只是不明白问题是什么,你们当中有人知道解决方案吗?

I had the same problem before and solved by adding this to my dialog class 我之前遇到过同样的问题,并通过将其添加到对话框类中来解决

public Dialog onCreateDialog(Bundle savedInstanceState) {
    Dialog dialog = super.onCreateDialog(savedInstanceState);
    dialog.getWindow().setSoftInputMode (WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    return dialog;

}

You said you used this code before but didn't mention where you used it. 您说过您曾经使用过此代码,但没有提及您在何处使用它。 Try it and let me know 试试看,让我知道

and to hide it after click close or finish use this code: 并在单击关闭或完成后隐藏它,请使用以下代码:

final InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getView().getWindowToken(), 0);

Basically having an alert dialog was my problem. 基本上,有一个警报对话框是我的问题。 It didn't let me set content view properly and using setView(view) was my downfall. 它没有让我正确设置内容视图,而使用setView(view)是我的失败。

By extending my custome dialog class to a dialog instead of an alert dialog and using setContentView() instead of setView()(as available in the alert dialog), I no longer have issues with the soft keyboard. 通过将我的自定义对话框类扩展到对话框而不是警报对话框,并使用setContentView()代替setView()(可在警报对话框中使用),软键盘不再出现问题。

First try: 第一次尝试:

 imm.showSoftInput(textView, InputMethodManager.SHOW_IMPLICIT);

If that does not work try this: 如果这样不起作用,请尝试以下操作:

Handler handler=new Handler();
Runnable r=new Runnable(){
        public void run() {
               imm.showSoftInput(textView, InputMethodManager.SHOW_IMPLICIT);
        }
}; 
handler.postDelayed(r, 100);

The second method will delay opening the keyboard 100 ms in case the view is still getting set up etc 第二种方法将在打开视图的情况下延迟100毫秒打开键盘等

The problem seems to be that since the place where you enter text is hidden initially (or nested or something), AlertDialog is automatically setting the flag WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM or WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE so that things don't trigger a soft input to show up. 问题似乎是由于输入文本的位置最初是隐藏的(或嵌套的或其他东西),因此AlertDialog自动设置了标志WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IMWindowManager.LayoutParams.FLAG_NOT_FOCUSABLE以使事情不会触发软输入露面。

The way to fix it is the following: 修复它的方法如下:

(...)
// Create the dialog and show it
Dialog dialog = builder.create()
dialog.show();

// After show, clear the flags and set the soft input mode
dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM