从另一个类调用对话框

4

我是Android的初学者,为了不在每个活动中重复编写对话框代码,我创建了一个包含所有显示对话框方法的类,以下是一个小代码片段:

public class Dialogues extends Activity implements DialogueMethods {


    public void showAlertDialog(Context context, String title, String message) {

        AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
        alertDialog.setTitle(title);
        alertDialog.setMessage(message);
        alertDialog.show();
}

    //this method am calling

    public void showAlertDialog(Context context, String title, String message, String ButtonText, boolean cancel) {

        AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
        alertDialog.setTitle(title);
        alertDialog.setMessage(message);

        if(cancel) {

            alertDialog.setNegativeButton(ButtonText, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
                finish();
            }
        });
        }

        alertDialog.show();
    }

}

我正在调用

//dialogObj is instance of the above class

dialogObj.showAlertDialog(MyActivity.this, "Error", "Not Connected to Internet", "Exit", true);

当我运行代码时,对话框是可见的,但按钮不可见,这是因为DialogInterace.onClickListener吗?我只是想知道这样做是否是一个好主意?如果是,那么正确的方法是什么?请帮帮我。谢谢。
2个回答

7

我想与你分享我的使用方法,可以创建类并在需要的地方调用。

public class DialogsUtil {

private Context mContext;

public DialogsUtil(Context context) {
    this.mContext = context;
}

/**
 * Return an alert dialog
 *
 * @param message  message for the alert dialog
 * @param listener listener to trigger selection methods
 */
public void openAlertDialog(Context context, String message, String positiveBtnText, String negativeBtnText,
                            final OnDialogButtonClickListener listener) {

    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setPositiveButton(positiveBtnText, (dialog, which) -> {
        dialog.dismiss();
        listener.onPositiveButtonClicked();

    });

    builder.setNegativeButton(negativeBtnText, (dialog, which) -> {
        dialog.dismiss();
        listener.onNegativeButtonClicked();

    });
    builder.setTitle(context.getResources().getString(R.string.app_name));
    builder.setMessage(message);
    builder.setIcon(android.R.drawable.ic_dialog_alert);
    builder.setCancelable(false);
    builder.create().show();
}

/**
 * return a dialog object
 * @return
 */
public Dialog openDialog(@LayoutRes int layoutId) {
    Dialog dialog = new Dialog(mContext);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
    dialog.setContentView(layoutId);
    dialog.getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    dialog.getWindow().setGravity(Gravity.BOTTOM);
    dialog.setCancelable(true);
    dialog.setCanceledOnTouchOutside(false);
    return dialog;
}
}

我创建了一个用于此对话框的界面,

public interface OnDialogButtonClickListener {

void onPositiveButtonClicked();

void onNegativeButtonClicked();
}

只需在您想使用对话框的活动中实现此接口,然后通过类对象的帮助,您可以像这样使用对话框:

 mDialogsUtil.openAlertDialog(YourActivity.this, "text message", "positive button msg", "Negative button msg", this);

您可以在您的活动中覆盖这两个方法。
@Override
public void onPositiveButtonClicked() {

}

//user clicked cancel.Close the application
@Override
public void onNegativeButtonClicked() {

}

谢谢,希望这对你有所帮助。


0
最好的方法是在一个基类中定义所有的对话框,然后调用它。
Class BaseActivity extends Activity{

int DIALOG_X = 1;
int DIALOG_Y = 2;
int DIALOG_Z = 3;
// More Dialog identifiers 

ProgressDialog progressDialog;
AlertDialog alertDialog;
//More dialog objects if you need

protected Dialog onCreateDialog(int id) {
    Dialog dialog;
    switch(id) {
    case DIALOG_X:
        // do the work to define the X Dialog
        break;
    case DIALOG_Y:
        // do the work to define the Y Dialog
        break;
    default:
        dialog = null;
    }
    return dialog;
}
}

然后另外一个类扩展BaseActivity并调用

showDialog(DIALOG_X); 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接