Android AlertDialog(提示视图)隐藏键盘

时间:2013-10-25 11:27:53

标签: android alertdialog

因为它是一个promptView而不是Activity,所以我不能去Manifest隐藏键盘。 我搜索了谷歌,我发现了类似的主题,但我无法弄清楚如何使这个工作。

LayoutInflater li = LayoutInflater.from(this);
View promptsView = li.inflate(R.layout.prompt_firstime, null);

final EditText nameInput = (EditText) promptsView.findViewById(R.id.prompt_name);
final EditText emailInput = (EditText) promptsView.findViewById(R.id.prompt_email);

InputMethodManager inputManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
inputManager.hideSoftInputFromWindow(nameInput.getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);
inputManager.hideSoftInputFromWindow(emailInput.getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);

AlertDialog alertDialog = new AlertDialog.Builder(Menu.this).create();  
alertDialog.setTitle("Title");  
alertDialog.setView(promptsView);
// etc

我做错了什么?感谢。

3 个答案:

答案 0 :(得分:0)

试试这个,它可以解决你的问题。

your_activityName.this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

答案 1 :(得分:0)

尝试此方法,只需在调用

时传递您的活动上下文
public static void hidekeypad(Activity activity)
    {
        @SuppressWarnings("static-access")
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(activity.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
    }

答案 2 :(得分:0)

尝试以下方法..它适用于我的情况:))

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(your_edit_text.getWindowToken(), 0);