在启动ProgressDialog之前隐藏键盘

时间:2015-12-30 03:58:31

标签: java android android-softkeyboard progressdialog

我有一个键盘,在启动下一个功能之前我绝对需要消失。我已经确定的是ProgressDialog正在阻碍它。我拿出了线,它工作正常。为什么ProgressDialog不能显示 当且仅当 键盘已被放好?它似乎只是有自己的想法。

public void hideKeyboard(){
    View view = this.getActivity().getCurrentFocus();
    if (view != null){
        InputMethodManager imm = (InputMethodManager)((this.getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)));
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);

    }

    ShowProgressDialog(getResources().getString(R.string.saving_design));
    new SaveTemplateTask().execute();
}

更新:这似乎是逻辑,但它仍然不起作用。 ProgressDialog根本不会等待hideKeyboard被验证为真。

我让hideKeyboard返回一个布尔值。

public boolean hideKeyboard() {
    View view = this.getActivity().getCurrentFocus();
    if (view != null) {
        InputMethodManager imm = (InputMethodManager) ((this.getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)));
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
    return true;
}

然后在一个单独的函数中:

    if (hideKeyboard()) {
        ShowProgressDialog(getResources().getString(R.string.saving_design));
        new SaveTemplateTask().execute();
    }

3 个答案:

答案 0 :(得分:0)

public static void hideKeyboard(Context ctx) {
        InputMethodManager inputManager = (InputMethodManager) ctx
                .getSystemService(Context.INPUT_METHOD_SERVICE);

        // check if no view has focus:
        View v = ((Activity) ctx).getCurrentFocus();
        if (v == null)
            return;

        inputManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
    }

答案 1 :(得分:0)

隐藏键盘非常简单。根据您的要求使用以下代码

getActivity().getWindow().setSoftInputMode(
            WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

答案 2 :(得分:0)

我放弃并使用了Toasts(一个在我的onCreate中,在我的新SaveTemplateTask的onPostExecute方法中...因为ProgressDialog倾向于在之前隐藏键盘(这不是我们想要的是什么。这真的只是看起来更好,所以我对警察说得好,但有趣的是要知道为什么上面我上面的问题描述的过程看起来并不顺利。< / p>

hideKeyboard();

if (hideKeyboard()) {
     ShowProgressDialog(getResources().getString(R.string.saving_design));
     Toast.makeText(getActivity().getApplicationContext(), "Saving design", Toast.LENGTH_LONG).show();
     new SaveTemplateTask().execute();
}

第二个功能:

public boolean hideKeyboard() {
    View view = this.getActivity().getCurrentFocus();
    if (view != null) {
        InputMethodManager imm = (InputMethodManager) ((this.getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)));
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        return true;
    }
    return false;
}