web-dev-qa-db-fra.com

DialogFragment et force d'afficher le clavier

J'ai un problème avec mon DialogFragment. Donc, pour créer ma vue, j'utilise la méthode décrite sur le Android blog. Voici mon DialogFragment

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final View myLayout = inflater.inflate(R.layout.dialog_connect, null);

    edit = (EditText) myLayout.findViewById(R.id.password_edit);
    edit.requestFocus();
    getDialog().getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);

    return myLayout;
}

Si j'utilise onCreateView (), cela fonctionne mais je voudrais créer un AlterDialog et pour ce faire, j'ai le code suivant:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the dialog layout
    builder
           .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    callback.onYesConnectClick(edit.getText().toString());
                }
            })
            .setNegativeButton(R.string.refuse, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    callback.onNoConnectClick();
                }
            });

    return builder.create();
}

Si je commente le code de onCreateView (), l'application fonctionne mais je ne peux pas forcer l'affichage du clavier et si je décommente onCreateView (), j'obtiens un plantage. Voici la trace de la pile:

Java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test/com.test.ProfileActivity_}: Android.util.AndroidRuntimeException:     requestFeature() must be called before adding content
AndroidRuntime at Android.app.ActivityThread.performLaunchActivity (ActivityThread.Java:2312)
AndroidRuntime at Android.app.ActivityThread.handleLaunchActivity (ActivityThread.Java:2362)
AndroidRuntime at Android.app.ActivityThread.access$600(ActivityThread.Java:156)
AndroidRuntime at Android.app.ActivityThread$H.handleMessage(ActivityThread.Java:1250)
AndroidRuntime at Android.os.Handler.dispatchMessage(Handler.Java:99)
AndroidRuntime at Android.os.Looper.loop(Looper.Java:137)
AndroidRuntime at Android.app.ActivityThread.main(ActivityThread.Java:5229)
AndroidRuntime at Java.lang.reflect.Method.invokeNative(Native Method)
AndroidRuntime at Java.lang.reflect.Method.invoke(Method.Java:525)
AndroidRuntime at com.Android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.Java:799)
AndroidRuntime at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:566)
AndroidRuntime at dalvik.system.NativeStart.main(Native Method)
AndroidRuntime Caused by: Android.util.AndroidRuntimeException: requestFeature() must be called before adding content

Donc ma question ==> Puis-je utiliser le AlertDialog et afficher le clavier lorsque la boîte de dialogue apparaît?

45
mrroboaat

remplacer onActivityCreated dans votre boîte de dialogue et mettre getDialog().getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE); dedans

122
tyczj

La réponse de tyczj ne fonctionne pas pour moi.

La solution était, à l'intérieur de onCreateDialog

Dialog d = builder.create();
d.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
return d;

Au final, le code serait comme ça

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the dialog layout
    builder
           .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    callback.onYesConnectClick(edit.getText().toString());
                }
            })
            .setNegativeButton(R.string.refuse, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    callback.onNoConnectClick();
                }
            });

    Dialog d = builder.create();
        d.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
return d;
}
18
Purgarcita

Utilisation "SOFT_INPUT_STATE_ALWAYS_VISIBLE" au lieu de "SOFT_INPUT_STATE_VISIBLE" soit dans la méthode onActivityCreated ou onCreateDialog.

11
Garfield

Méfiez-vous de l'appel setLayout() si vous l'utilisez. Il m'a fallu un certain temps pour réaliser qu'il pouvait remplacer les attributs de votre fenêtre. Après l'avoir enveloppé dans un gestionnaire, la solution acceptée a fonctionné pour moi.

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    final Dialog dialog = super.onCreateDialog(savedInstanceState);
    dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

    return dialog;
}

@Override
public void onStart() {
    super.onStart();

    // without a handler, the window sizes itself correctly
    // but the keyboard does not show up
    new Handler().post(new Runnable() {
        @Override
        public void run() {
            getDialog().getWindow().setLayout(DIALOG_WIDTH, DIALOG_HEIGHT);
        }
    });
}
6
SqueezyMo