web-dev-qa-db-fra.com

Comment utiliser Dialog Fragment? (showDialog obsolète) Android

Je comprends qu'il y a cette documentation

http://developer.Android.com/reference/Android/app/DialogFragment.html#AlertDialog

mais en tant que nouvel apprenant Android/Java, il n'est pas facile de comprendre la quantité de code impliquée par l'écriture d'un simple dialogue d'alerte qui apparaît avec 2 options (oui/non).

Voici le code que j'ai actuellement dans mon fichier MainActivity:

final private int RESET_DIALOG = 0;

    private OnClickListener resetButtonListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            showDialog(RESET_DIALOG);

        }
    };

    protected Android.app.Dialog onCreateDialog(int id) {
        switch(id) {
        case RESET_DIALOG: 
            AlertDialog.Builder builder = new Builder(this);
            return builder
                    .setMessage("Are you sure you want to reset the count?")
                    .setNegativeButton("No", new DialogInterface.OnClickListener() {    

                        @Override
                        public void onClick(DialogInterface arg0, int arg1) {
                            Toast.makeText(MainActivity.this, "Did not reset!", 5).show();

                        }
                    })

                    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {


                        @Override
                        public void onClick(DialogInterface arg0, int arg1) {
                            Toast.makeText(MainActivity.this, "Did Reset!", 5).show();

                        }
                    })
                    .create();
        }
        return null;
    };

Voici ma tentative de suivre les instructions sur le site Android: Fichier d’activité principale:

final private int RESET_DIALOG = 0;

    private OnClickListener resetButtonListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, MainDialog.class);
            startActivity(intent);

        }
    };

    protected Android.app.Dialog onCreateDialog(int id) {
        switch(id) {
        case RESET_DIALOG: 
            AlertDialog.Builder builder = new Builder(this);
            return builder
                    .setMessage("Are you sure you want to reset the count?")
                    .setNegativeButton("No", new DialogInterface.OnClickListener() {    

                        @Override
                        public void onClick(DialogInterface arg0, int arg1) {
                            Toast.makeText(MainActivity.this, "Did not reset!", 5).show();

                        }
                    })

                    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {


                        @Override
                        public void onClick(DialogInterface arg0, int arg1) {
                            Toast.makeText(MainActivity.this, "Did Reset!", 5).show();

                        }
                    })
                    .create();
        }
        return null;
    };

Ensuite, j'ai créé une classe MainDialog: (Je ne sais plus comment l’appliquer correctement ni l’appliquer)

package com.proteintracker;

import Android.support.v4.app.DialogFragment;

public class MainDialog extends DialogFragment {
    public static MyAlertDialogFragment newInstance(int title) {
        MyAlertDialogFragment frag = new MyAlertDialogFragment();
        Bundle args = new Bundle();
        args.putInt("title", title);
        frag.setArguments(args);
        return frag;
    }
}

Je ne sais pas si j'étais supposé créer une nouvelle classe pour le fragment et comment l'appliquer à ma boîte de dialogue actuelle dans l'écran d'activité.

23
Lion789

Vous pouvez afficher votre DialogFragment comme ceci:

void showDialog() {
    DialogFragment newFragment = MyAlertDialogFragment.newInstance(
            R.string.alert_dialog_two_buttons_title);
    newFragment.show(getFragmentManager(), "dialog");
}

Dans votre boîte de dialogue de fragmentation, vous devez remplacer onCreateDialog et vous renvoyer une instance de simple Dialog, par exemple AlertDialog.

public static class MyAlertDialogFragment extends DialogFragment {

public static MyAlertDialogFragment newInstance(int title) {
    MyAlertDialogFragment frag = new MyAlertDialogFragment();
    Bundle args = new Bundle();
    args.putInt("title", title);
    frag.setArguments(args);
    return frag;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    int title = getArguments().getInt("title");

    AlertDialog.Builder builder = new Builder(this);
    return builder
                .setMessage("Are you sure you want to reset the count?")
                .setNegativeButton("No", new DialogInterface.OnClickListener() {    

                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        Toast.makeText(MainActivity.this, "Did not reset!", 5).show();

                    }
                })

                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {


                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        Toast.makeText(MainActivity.this, "Did Reset!", 5).show();

                    }
                })
                .create();
}
}
25
HotIceCream

Alerte avec vue personnalisée

public class MyAlertDialogFragment extends DialogFragment {

    public static final String TITLE = "dataKey";

    public static MyAlertDialogFragment newInstance(String dataToShow) {
        MyAlertDialogFragment frag = new MyAlertDialogFragment();
        Bundle args = new Bundle();
        args.putString(TITLE, dataToShow);
        frag.setArguments(args);
        return frag;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        String mDataRecieved = getArguments().getString(TITLE,"defaultTitle");

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.alert_layout, null);

        TextView mTextView = (TextView) view.findViewById(R.id.textview);
        mTextView.setText(mDataRecieved);
        setCancelable(false);

        builder.setView(view);
        Dialog dialog = builder.create();

        dialog.getWindow().setBackgroundDrawable(
                new ColorDrawable(Color.TRANSPARENT));

        return dialog;

    }
}

Et alerte avec l'interface YesNoDialog

public class MyAlertDialogFragment extends DialogFragment {

    public static final String TITLE = "dataKey";
    private OnYesNoClick yesNoClick;

    public static MyAlertDialogFragment newInstance(String dataToShow ) {
        MyAlertDialogFragment frag = new MyAlertDialogFragment();
        Bundle args = new Bundle();
        args.putString(TITLE, dataToShow);
        frag.setArguments(args);
        return frag;
    }

    public void setOnYesNoClick(OnYesNoClick yesNoClick) {
        this.yesNoClick = yesNoClick;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        String mDataRecieved = getArguments().getString(TITLE,"defaultTitle");

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        builder
                .setMessage("Message to Show")
                .setNegativeButton("No", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        if(yesNoClick != null)
                            yesNoClick.onNoClicked();
                    }
                })

                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        if(yesNoClick != null)
                            yesNoClick.onYesClicked();
                    }
                });
        Dialog dialog = builder.create();

        dialog.getWindow().setBackgroundDrawable(
                new ColorDrawable(Color.TRANSPARENT));

        return dialog;

    }

    public interface OnYesNoClick{
        void onYesClicked();
        void onNoClicked();
    }
}

Utilisez-le comme

private void showYesNoDialog(){
        MyAlertDialogFragment yesNoAlert = MyAlertDialogFragment.newInstance(
                "Data to Send");
        yesNoAlert.show(getFragmentManager(), "yesNoAlert");

        yesNoAlert.setOnYesNoClick(new MyAlertDialogFragment.OnYesNoClick() {
            @Override
            public void onYesClicked() {
                //yes or ok clicked
            }

            @Override
            public void onNoClicked() {
                //no or cancel clicked
            }
        });
    }
7
Xar E Ahmer

Exemple de dialogue DialogFragment utilisant Sherlock

FragmentManager fm = getSherlockActivity().getSupportFragmentManager();
DialogFragment dialog = new DialogFragment(){
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the Builder class for convenient dialog construction
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder
            .setTitle(getString(R.string.delete)+"?")
            .setPositiveButton(getString(Android.R.string.ok), new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                // do something
                }
            })
            .setNegativeButton(getString(Android.R.string.cancel),  new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dismiss();
                }
            });
        // Create the AlertDialog object and return it
        return builder.create();
    }
};
dialog.setCancelable(true);
dialog.show(fm, "DELETE_DIALOG_FRAGMENT");
2
iflorit

Vous pouvez afficher le dialogue comme ceci: 

new AlertDialog.Builder(this)
.setMessage("Are you sure you want to reset the count?")
.setNegativeButton("No", new DialogInterface.OnClickListener() {    

    @Override
    public void onClick(DialogInterface arg0, int arg1) {
        Toast.makeText(MainActivity.this, "Did not reset!", 5).show();
    }
})
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface arg0, int arg1) {
        Toast.makeText(MainActivity.this, "Did Reset!", 5).show();

    }
})
.create().show();
0
Sapan Diwakar