在显示DialogFragment之前隐藏键盘

时间:2013-08-28 16:30:44

标签: android

我有第一个活动A,用户在其中设置登录名和密码。然后他点击“确定”,键盘打开,他到达活动B.在活动B的onCreate中,我显示一个DialogFrament但键盘仍然打开,直到我隐藏DialogFragment。

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    GeolocalisationDialog geoDlg = new GeolocalisationDialog();
    geoDlg.show(getSupportFragmentManager(), "geoDlg");
}

我的问题是如何在显示DialogFragment之前隐藏键盘?

ps:我的DialogFrament是一个基本的DialogFragment

public class GeolocalisationDialog extends DialogFragment {

private static DialogClickListener callback;

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
        callback = (DialogClickListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString() + " must implement DialogClickListener");
    }
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setMessage(R.string.authorize_geoloc)
           .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    callback.onYesClick();
                }
            })
            .setNegativeButton(R.string.refuse, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    callback.onNoClick();
                }
            });

    return builder.create();
}

}

ps2:我曾尝试在清单中设置android:windowSoftInputMode =“stateHidden”并且它不起作用

1 个答案:

答案 0 :(得分:0)

使用以下方法强行隐藏软键盘:

InputMethodManager imm = (InputMethodManager)getSystemService(
      Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(yourEditText.getWindowToken(), 0);
// here you may choose the editText where the last time keyboard was opened. 

有关详情,请参阅How to make the virtual keyboard go away?