如何调用通用活动完成
我有一个公共类,它具有一些常见的通用功能,例如用于在我的应用程序中显示对话框。我创建了一个通用的 alertButtonDialog
函数,并希望在使用对话框时在活动中调用它。我对 Java 很陌生,所以如果它非常基础,请原谅。
public static class AlertDialogs{
public static void alertButtonDialog(Activity activity, Context context, String title, String message,
String positiveButton, String negativeButton) {
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
alertBuilder.setTitle(title);
alertBuilder.setMessage(message);
alertBuilder.setPositiveButton(positiveButton, new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Activity.this.finish(); // *?? How to do this part ??*
// the activity to be finished is the activity which calls this function
}
})
}
}
稍后在任何其他活动中,每当我显示对话框时,我都会这样做
AlertDialogs.alertButtonDialog(...all my Strings...)
这只是为了方便访问。
I have a public class which has some common generic functions, like e.g. for displaying dialogs in my application. I made a generic alertButtonDialog
function and want to call it in activities whenever I'm using dialogs. I am very new to Java so please excuse me if it's very basic.
public static class AlertDialogs{
public static void alertButtonDialog(Activity activity, Context context, String title, String message,
String positiveButton, String negativeButton) {
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
alertBuilder.setTitle(title);
alertBuilder.setMessage(message);
alertBuilder.setPositiveButton(positiveButton, new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Activity.this.finish(); // *?? How to do this part ??*
// the activity to be finished is the activity which calls this function
}
})
}
}
Later in any other activity, whenever I'm displaying a dialog, I would just do
AlertDialogs.alertButtonDialog(...all my Strings...)
This is only for convenience accessing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
alertButtonDialog
函数中,将Activity Activity
参数定义为最终参数。然后,从onClick
侦听器中调用activity.finish();
。请注意,您不需要同时使用
Activity
和Context
- 因为前者扩展了后者 - 除非您希望将 Activity 作为 null 传递到某处。In your
alertButtonDialog
function, define theActivity activity
argument as final. Then, from within theonClick
listener invokeactivity.finish();
.Note that you do not need both an
Activity
and aContext
-since the former extends the later-, unless you are expecting to pass activity as null somewhere.