2

我尝试使用布尔值作为结果创建自己的确认对话框类。我已经阅读了任何教程或建议,但结果不是我想要的。

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

德尔福:

if MessageDlg ('Message Text', 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;
}

问题是我是否可以创建一个生成确认对话的类,该类将产生一个布尔值真或假,就像在 Delphi 中一样。

确认对话框,以便该类可以在课堂或其他活动中使用

4

4 回答 4

3

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;
于 2012-11-21T16:52:48.203 回答
2

您可以为对话框使用自定义布局,或者对于一些更复杂的需求,创建以对话框为主题的 Activity 并使用startActivityForResults(). 只需将其添加到活动清单条目中,即可设计您的“对话” UI 和主题活动:

android:theme="@android:style/Theme.Dialog"
于 2012-11-18T17:22:20.800 回答
0

尝试使用cordova,最新的稳定版本是2.1.0 它有一个navigator.notification(在这种情况下是alert或confirm)API

http://docs.phonegap.com/en/2.1.0/guide_getting-started_index.md.html#Getting%20Started%20Guides

http://docs.phonegap.com/en/2.1.0/cordova_notification_notification.md.html#Notification 唯一的缺点是,对于黑莓,本机应用程序更容易出错

于 2012-11-19T12:51:06.357 回答
0

好的。所以你想要一个方法或类,你只需要传递值,每次你需要一个对话框时都会调用它。我制作了一个接收短信并将其显示在对话框中的程序。您只需要将值作为意图传递并做任何您喜欢的事情。它是一个简单的程序,您可以根据需要进行修改。 http://zohaibbrohi.blogspot.com/2012/11/recieve-sms-and-show-it-in-activity-as.html

于 2012-11-18T18:53:38.617 回答