Android:软键盘打开和;使用/多个编辑文本隐藏自定义对话框

时间:2013-11-27 22:19:36

标签: android dialog keyboard android-edittext

好的 - 我已经把这个问题搞得一团糟了。我有一个自定义对话框,有多个edittexts。我已经设置了我的布局,因此它不会自动聚焦于第一个,因为它们最初都填充了默认值(因此用户可能只需按“接受”)

我希望任何edittext在触摸时清除自己并打开键盘进行数字输入。他们可能会更改一个或两个字段。如果他们触摸并且未输入,则该字段应更改回默认值。

除了addTextChangedListeners之外,我还使用setOnFocusChangedListeners实现了这个功能。 我的第一个问题是,当用户触摸两个edittexts时,键盘正在切换自己打开/关闭。我通过使用SHOW_FORCED,HIDE_NOT_ALWAYS作为toggleSoftInput的参数解决了这个问题。请注意,这是唯一一组在触摸第二个字段时保持键盘打开的参数。

不幸的是,这造成了第二个问题,我不明白 - 在退出对话框时,我无法再关闭键盘(即,它在下面的视图中仍然可见)。以前,当我没有努力清除输入时,键盘关闭了。使用SHOW_IMPLICIT(忽略切换问题)也可以在退出时关闭打开的键盘。

那么......我怎么做到这一点?

以下是一些相关的代码部分:

            edQuantity.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    Log.i(TAG,"edQuantity focus changed");
                    if (hasFocus) {
                        Log.i(TAG,"edQuantity HAS FOCUS");
                        edQuantity.setHint("");
                        edQuantity.setText("");
                        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,InputMethodManager.HIDE_NOT_ALWAYS);
                    }

                    // was the other field left empty after a change attempt?
                    if (String.valueOf(edPrice.getText()).length()==0) edPrice.setText("0.01");  
                }
            });


            edPrice.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    Log.i(TAG,"edPrice focus changed");
                    if (hasFocus) {
                        Log.i(TAG,"edPrice HAS FOCUS");
                        edPrice.setHint("");
                        edPrice.setText("");
                       InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
                       imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,InputMethodManager.HIDE_NOT_ALWAYS);
                    }

                    // was the other field left empty after a change attempt?
                    if (String.valueOf(edQuantity.getText()).length()==0) edQuantity.setText("1");
                }
            });


protected static void dismissCustomDialog(Dialog dialog, Context context) {

    if (dialog != null) {

        // hide the soft keyboard
        if (dialog.getCurrentFocus() != null) {

            Log.i(TAG,"trying to hide a keyboard");
            InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(dialog.getWindow().getDecorView().getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);
            imm.hideSoftInputFromWindow(dialog.getWindow().getDecorView().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }

        if(dialog.isShowing()) dialog.dismiss();

    }
}

1 个答案:

答案 0 :(得分:0)

根据我上面的评论,使用切换来查看键盘是否已经打开。