简体   繁体   English

转到非活动课程中的上一个活动

[英]Go to previous activity in non-activity class

I got a problem I made a class for Alert Dialogs now if someone presses Ok it should go back to the previous activity but I don't know how to do this, because when i put in finsih(); 我遇到一个问题,如果有人按“确定”,我现在就为“警报对话框”创建一个类,它应该返回到上一个活动,但是我不知道如何执行此操作,因为当我放入finsih()时; it gives me an error here is my code: 这给我一个错误,这是我的代码:

package com.laurenswuyts.find.it;

import com.laurenswuyts.find.it.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;



public class AlertDialogManager {
    /**
     * Function to display simple Alert Dialog
     * @param context - application context
     * @param title - alert dialog title
     * @param message - alert message
     * @param status - success/failure (used to set icon)
     *               - pass null if you don't want icon
     * */
    @SuppressWarnings("deprecation")
    public void showAlertDialog(Context context, String title, String message,
            Boolean status) {
        AlertDialog alertDialog = new AlertDialog.Builder(context).create();

        // Setting Dialog Title
        alertDialog.setTitle(title);

        // Setting Dialog Message
        alertDialog.setMessage(message);

        if(status != null)
            // Setting alert dialog icon
            alertDialog.setIcon((status) ? R.drawable.success : R.drawable.fail);

        // Setting OK Button
        alertDialog.setButton("OK", new DialogInterface.OnClickListener() {

            public void onClick(final DialogInterface dialog, final int which) {

            }





        });

        // Showing Alert Message
        alertDialog.show();
    }
}

In the public void Onclick i tried to type finish(); 在公共无效的Onclick中,我尝试键入finish();。 but that didnt work. 但这没用。

Can anyone help me? 谁能帮我? Thanks in advance! 提前致谢!

Regards, 问候,

You should add a property to your manager; 您应该向经理添加属性;

Context context;

Init it on your showAlertDialog() method. showAlertDialog()方法上初始化它。

Under your click; 在您的点击下;

((Activity) context).finish();

You could pass the click listener in from the calling activity: 您可以通过调用活动传递点击侦听器:

DialogInterface.OnClickListener clickListener = new DialogInterface.OnClickListener() {
        public void onClick(final DialogInterface dialog, final int which) {
            finish();
        }
    });
AlertDialogManager manager = new AlertDialogManager();
manager.showAlertDialog(this, title, message, status, clickListener);

Then in your AlertDialogManager , you change the method like this: 然后,在AlertDialogManager ,更改方法如下:

public void showAlertDialog(Context context, String title, String message,
        Boolean status, DialogInterface.OnClickListener clickListener) {
    AlertDialog alertDialog = new AlertDialog.Builder(context).create();

    // Setting Dialog Title
    alertDialog.setTitle(title);

    // Setting Dialog Message
    alertDialog.setMessage(message);

    if(status != null)
        // Setting alert dialog icon
        alertDialog.setIcon((status) ? R.drawable.success : R.drawable.fail);

    // Setting OK Button
    alertDialog.setButton("OK", clickListener);

    // Showing Alert Message
    alertDialog.show();
}

This way the click behaviour is handled by the calling class, and the AlertDialogManager doesn't have any knowledge of what happens after OK is clicked. 这样,单击行为由调用类处理,并且AlertDialogManager不了解单击确定后会发生什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM