how to reuse android alertdialog

北城余情 提交于 2019-12-10 18:48:00

问题


I want to reuse the code for alertDialog and put it in another java file as function call . But "this" cannot be used to replace the "MyActivity.this"? How to pass it as a parameter? Best if the code is generic.

    AlertDialog alertDialog = new AlertDialog.Builder(MyActivity.this).create();
            alertDialog.setTitle("Alert");
            alertDialog.setMessage("Alert message to be shown");
            alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
            alertDialog.show();

回答1:


You can use something like this in a separate class, for example I have used AlertUtils.java:

public class AlertUtils
{
    public static void showOKDialog(Context context, String title, String message)
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(title);
        builder.setMessage(message);
        builder.setPositiveButton(android.R.string.ok, null);
        builder.show();
    }
}

In this method, the Context you pass through could be your Activity's this, eg: MyActivity.this or a fragment's getContext()

AlertUtils.showOKDialog(MyActivity.this, "title of dialog", "message to display in dialog");




回答2:


public class Utils {
    public void showDialog(Context context) {
        AlertDialog alertDialog = new AlertDialog.Builder(context).create();
        alertDialog.setTitle("Alert");
        alertDialog.setMessage("Alert message to be shown");
        alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
        alertDialog.show();
    }
}

In your Activity, you can reuse show dialog by

new Utils().showDialog(MyActivity.this);




回答3:


You can write your alert dialog code seperately in a class as below :

public class Utils{
  public static void showMessage(final Activity activity, String title, String posText){

    MaterialDialog dialog = new MaterialDialog.Builder(activity)
            .content(title)
            .positiveText(posText)
            .onPositive(new MaterialDialog.SingleButtonCallback() {
                @Override
                public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {

                    dialog.dismiss();
                }
            })
            .build();
    dialog.show();

}
}

And from your activity / fragment you can call this as :

Activity,

AppUtils.showMessage(this, getString("your text"), getString("your text"));

Fragment,

AppUtils.showMessage(getActivity(),getString("your text"), getString("your text"));

Import Utils class in both fragment or activity then everything will work perfectly.

Hope this helps you to get rid of your problem.



来源:https://stackoverflow.com/questions/38194234/how-to-reuse-android-alertdialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!