在 Android 中创建可重用对话框的最佳方式是什么?
通读 Dialog dev guide ,我知道我可以在我的一个 Activity
中使用 AlertDialog.Builder(this);
,但是如果我想在多个 Activity 中使用它怎么办?如果这是其他一些类,我会扩展它,所以 MyDialog extends AlertDialog
,但是我不能使用 Builder
。
有什么建议吗?
最佳答案
创建一个类文件,如 AllMethod.java
并将此代码添加到该类文件中。
public static void showAlert(Activity act, String msg, DialogInterface.OnClickListener listener) {
AlertDialog.Builder alert = new AlertDialog.Builder(act);
alert.setMessage(msg);
alert.setPositiveButton("OK", listener);
alert.show();
}
你可以像下面的代码一样使用任何类。
AllMethod.showAlert(mActivity, "", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Do your code for click
}
});
关于android - 拥有可重用对话框的最佳方式是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6026571/