[英]Go to previous activity in non-activity class
我遇到一個問題,如果有人按“確定”,我現在就為“警報對話框”創建一個類,它應該返回到上一個活動,但是我不知道如何執行此操作,因為當我放入finsih()時; 這給我一個錯誤,這是我的代碼:
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();
}
}
在公共無效的Onclick中,我嘗試鍵入finish();。 但這沒用。
誰能幫我? 提前致謝!
問候,
您應該向經理添加屬性;
Context context;
在showAlertDialog()
方法上初始化它。
在您的點擊下;
((Activity) context).finish();
您可以通過調用活動傳遞點擊偵聽器:
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);
然后,在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();
}
這樣,單擊行為由調用類處理,並且AlertDialogManager
不了解單擊確定后會發生什么。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.