1

感谢您提供了一个了不起的资源,我有一个关于实现 android sdk apidemo AlertDialogSamples.java 的问题。基本上,我想将 AlertDialogSamples 类添加到我的应用程序中,并从我的任何活动中调用它以获取各种警报对话框(即DIALOG_YES_NO_MESSAGEDIALOG_PROGRESS...)。我很惊讶所有在线示例在每个活动类中都有 alertdialog 代码。

我在这个想法上尝试了一些迭代:

public class My1Activity extends SherlockActivity implements View.OnClickListener {
   ...
   AlertDialogSamples alert = new AlertDialogSamples();
   ...
   private void changeEmailCommand(){
         alert.showAlertDialog(My1Activity.this, DIALOG_TEXT_ENTRY, title);

接着:

public class AlertDialogSamples extends Activity {
   ...
public void showAlertDialog(Context context, int i, String title) {
        showAlertDialog(context, i, title);
  ... (rest is existing code and modifying the .setTitle to accept title message)
    }

并且没有运气让警报对话框出现(只是崩溃)

有没有办法用意图来做到这一点?异步?

4

3 回答 3

0
public static void showAlerDialog(Context context, String title,
        String msg, AlertDialogDelegate delegate, int code) {
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
    alertDialog.setTitle(title);
    alertDialog.setMessage(msg);
    alertDialog.setCancelable(false);

    if (title.equalsIgnoreCase(ERROR)) {
        alertDialog.setIcon(context.getResources().getDrawable(
                R.drawable.error_icon));
    } else if (title.equalsIgnoreCase(SUCCESS)) {
        alertDialog.setIcon(context.getResources().getDrawable(
                R.drawable.success));
    } else {
        alertDialog.setIcon(context.getResources().getDrawable(
                R.drawable.warning));
    }

    alertDialog.setPositiveButton("OK", new AlertDialogListenter(delegate,
            code));
    alertDialog.show();

}
于 2012-11-29T12:05:54.823 回答
0

我使用以下代码显示进度对话框:

  1. 使用静态方法创建实用程序类。
  2. 当您想显示对话时,从您的活动中调用它们: Utility.showActionIndicator("Your message", Acitivty.this); 和 Utility.dismissActionIndicator();

你可以参考这个例子,试试运气。

更新代码以添加实用程序方法以显示警报对话框 - 只需调用 Utility.showAlertDialog(YourActivity.this)

更新:(提示 - 将 Morfy A Wounded Lion 的方法添加到此类 - 感谢 Morfy A Wounded Lion 的代码。太棒了)

public class Utility {

    private static ProgressDialog   actionIndicator = null;
    private static AlertDialog.Builder alertDialogBuilder   = null;

    private Utility() {

    }

    public static void showAlertDialog(Activity activity) {

        if (alertDialogBuilder == null) {
            alertDialogBuilder = new AlertDialog.Builder(activity);
        }
            // set title
            alertDialogBuilder.setTitle("Your Title");

            // set dialog message
            alertDialogBuilder
                .setMessage("Click yes to exit!")
                .setCancelable(false)
                .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {

                    }
                  })
                .setNegativeButton("No",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {

                        dialog.cancel();
                    }
                });

                // create alert dialog
                AlertDialog alertDialog = alertDialogBuilder.create();

                // show it
                alertDialog.show();

    }

    public static void showActionIndicator(String message,Activity activity) {

        if (actionIndicator == null) {
            actionIndicator = 
            new ProgressDialog(activity);
        }
        actionIndicator.setCancelable(false);
        actionIndicator.setCanceledOnTouchOutside(false);
        actionIndicator.setMessage(message);
        if (!actionIndicator.isShowing()) {
            try {
                actionIndicator.show();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                Log.d("exception","something went wrong",e);
            }
        }
        System.out.println("Dialog is showing");
    }

    public static void changeActionIndicatorMessage(String message) {

        if (actionIndicator != null && actionIndicator.isShowing()) {
            actionIndicator.setMessage(message);
        }
    }

    public static void dismissActionIndicator() {
        if (actionIndicator != null && actionIndicator.isShowing()) {
            try {
                actionIndicator.dismiss();
            } catch (Exception e) {
                e.printStackTrace();
            }
            actionIndicator = null;
            System.out.println("Dialog dismissed");
        }
    }

}
于 2012-11-07T10:56:11.100 回答
0
public static void showAlerDialog(Context context, String title,
        String msg, AlertDialogDelegate delegate, int code) {
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
    alertDialog.setTitle(title);
    alertDialog.setMessage(msg);
    alertDialog.setCancelable(false);

    if (title.equalsIgnoreCase(ERROR)) {
        alertDialog.setIcon(context.getResources().getDrawable(
                R.drawable.error_icon));
    } else if (title.equalsIgnoreCase(SUCCESS)) {
        alertDialog.setIcon(context.getResources().getDrawable(
                R.drawable.success));
    } else {
        alertDialog.setIcon(context.getResources().getDrawable(
                R.drawable.warning));
    }

    alertDialog.setPositiveButton("OK", new AlertDialogListenter(delegate,
            code));
    alertDialog.show();

}

然后像这样调用 AlertDialog

ClassName.showAlerDialog(context,title,msg,delegate,code);

这里上下文->上下文

标题 --> 警报对话框标题 -- 字符串

msg ---> alertdialog msg -- 字符串

委托 ---> 在某些情况下调用函数的接口对象是 -- 没有事件

code -- integer -- 代表一些不同的动作

于 2012-11-07T11:01:27.837 回答