gpt4 book ai didi

android - 在android上创建确认对话框类

转载 作者:太空狗 更新时间:2023-10-29 16:43:49 24 4
gpt4 key购买 nike

我尝试制作自己的带有 bool 值的确认对话框类作为结果。我已阅读任何教程或建议,但结果并不如我所愿。

问题是我讨厌写长代码,我想写一个简单的代码。这个对比delphi和android写确认对话框

德尔福:

if MessageDlg ('消息文本', mtConfirmation, [mbYes, mbNo], 0)

安卓:

boolean answer = false;

public boolean Confirm (Activity act, String Title, String ConfirmText,
CancelBtn String, String OkBtn) {
AlertDialog dialog = new AlertDialog.Builder (act). Create ();
dialog.setTitle (Title);
dialog.setMessage (ConfirmText);
dialog.setCancelable (false);
dialog.setButton (DialogInterface.BUTTON_POSITIVE, OkBtn,
new DialogInterface.OnClickListener () {
public void onClick (DialogInterface dialog, int buttonId) {
answer = true;
}
});
dialog.setButton (DialogInterface.BUTTON_NEGATIVE, CancelBtn,
new DialogInterface.OnClickListener () {
public void onClick (DialogInterface dialog, int buttonId) {
answer = false;
}
});
dialog.setIcon (android.R.drawable.ic_dialog_alert);
return answer;
}

问题是我能否创建一个生成确认对话的类,将生成一个 bool 值 true 或 false,就像在 Delphi 中一样。

确认对话框,以便该类(class)可以在类或其他 Activity 中使用

最佳答案

Android 并不总是传统的。您不能显示一个对话框并等待它显示,然后再做一些事情。

您需要显示一个对话框,并在回调中响应是/否按钮。

这样的事情是一种解决方案:

/**
* Display a confirm dialog.
* @param activity
* @param title
* @param message
* @param positiveLabel
* @param negativeLabel
* @param onPositiveClick runnable to call (in UI thread) if positive button pressed. Can be null
* @param onNegativeClick runnable to call (in UI thread) if negative button pressed. Can be null
*/
public static final void confirm(
final Activity activity,
final int title,
final int message,
final int positiveLabel,
final int negativeLabel,
final Runnable onPositiveClick,
final Runnable onNegativeClick) {

AlertDialog.Builder dialog = new AlertDialog.Builder(activity);
dialog.setTitle(title);
dialog.setMessage(message);
dialog.setCancelable (false);
dialog.setPositiveButton(positiveLabel,
new DialogInterface.OnClickListener () {
public void onClick (DialogInterface dialog, int buttonId) {
if (onPositiveClick != null) onPositiveClick.run();
}
});
dialog.setNegativeButton(negativeLabel,
new DialogInterface.OnClickListener () {
public void onClick (DialogInterface dialog, int buttonId) {
if (onNegativeClick != null) onNegativeClick.run();
}
});
dialog.setIcon (android.R.drawable.ic_dialog_alert);
dialog.show();

}

}

要使用上述方法,可以使用类似这样的东西:

        Alert.confirm(activity, R.string.titFileExport, R.string.lblFileConfirmOverwrite, 
R.string.yes, R.string.no,
new Runnable() { public void run() {performExport(activity, app, currentSong, saveFile);}},
null);
return;

关于android - 在android上创建确认对话框类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13442252/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com