0

我有第一个活动 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" 但它不起作用

4

1 回答 1

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. 

请参阅如何使虚拟键盘消失?更多。

于 2013-08-28T16:36:09.523 回答