自定义对话框等待响应

时间:2013-02-26 20:13:57

标签: android dialog return customdialog

我创建了一个自定义对话框,用于创建一个标准对话框,我可以在一行中创建一个标准对话框,这将是标准的。使用参数我更改文本。对话框非常简单,只有一个按钮。

现在我正在重新思考这是一个好主意。我实际上希望我的应用程序停止,直到我的对话框显示。我怎么能管理呢?给对话框一个返回类型?或者有更好的方法吗?

我的对话框:

/**
 * custom dialog
 * 
 * @param mcontext use activityname.this
 * @param title
 * @param text
 * @param button
 */
 public void showDialog(Context mcontext, String title,String text, String button) {
     // fonts
     Typeface tf_hn = Typeface.createFromAsset(mcontext.getAssets(), "helveticaneue.ttf");
     Typeface tf_hn_bold = Typeface.createFromAsset(mcontext.getAssets(), "helveticaneuebd.ttf");
     Resources res = mcontext.getResources();

     // custom dialog
     final Dialog dialog = new Dialog(mcontext);
     dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); //not the normal dialog title
     dialog.setContentView(R.layout.view_dialog);

     TextView tv_dialog_title = (TextView) dialog.findViewById(R.id.tv_dialog_title);
     tv_dialog_title.setText(title);
     tv_dialog_title.setTypeface(tf_hn_bold);
     tv_dialog_title.setTextColor(res.getColor(R.color.white));

     TextView tv_dialog_text = (TextView) dialog.findViewById(R.id.tv_dialog_text);
     tv_dialog_text.setText(text);
     tv_dialog_text.setTypeface(tf_hn);
     tv_dialog_text.setTextColor(res.getColor(R.color.white));

     Button dialogButton = (Button) dialog.findViewById(R.id.bt_dialog_button);
     dialogButton.setTypeface(tf_hn_bold);
     dialogButton.setText(button);
     dialogButton.setTextColor(res.getColor(R.color.white));
     // if button is clicked, close the custom dialog
     dialogButton.setOnClickListener(new OnClickListener() {
         @Override
         public void onClick(View v) { 
             dialog.dismiss();
         }
     });

     dialog.show();
}

然后我可以像这样使用它:

dialogH.showDialog(LoginActivity.this, res.getString(R.string.txt_dialog_fout), res.getString(R.string.txt_dialog_not_connected),res.getString(R.string.txt_dialog_button));

一切正常,直到我想显示一个“你已登录”(或左右)的对话框,然后在点击显示后启动一个意图。有人有想法吗?

1 个答案:

答案 0 :(得分:3)

使用内置侦听器创建一个自定义对话框类。

public class MyDialog extends Dialog {
    String title;
    String text;
    String button;

    DialogListener listener;

    interface DialogListener {
        void onCompleted();

        void onCanceled();
    }

    public MyDialog(Context context, String title, String text, String button) {
        super(context);
        this.title = title;
        this.text = text;
        this.button = button;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Typeface tf_hn = Typeface.createFromAsset(getContext().getAssets(), "helveticaneue.ttf");
        Typeface tf_hn_bold = Typeface.createFromAsset(getContext()..getAssets(), "helveticaneuebd.ttf");
        Resources res = getContext().getResources();

        requestWindowFeature(Window.FEATURE_NO_TITLE); // not the normal dialog title
        setContentView(R.layout.view_dialog);

        TextView tv_dialog_title = (TextView) findViewById(R.id.tv_dialog_title);
        tv_dialog_title.setText(title);
        tv_dialog_title.setTypeface(tf_hn_bold);
        tv_dialog_title.setTextColor(res.getColor(R.color.white));

        TextView tv_dialog_text = (TextView) findViewById(R.id.tv_dialog_text);
        tv_dialog_text.setText(text);
        tv_dialog_text.setTypeface(tf_hn);
        tv_dialog_text.setTextColor(res.getColor(R.color.white));

        Button dialogButton = (Button) findViewById(R.id.bt_dialog_button);
        dialogButton.setTypeface(tf_hn_bold);
        dialogButton.setText(button);
        dialogButton.setTextColor(res.getColor(R.color.white));
        // if button is clicked, close the custom dialog
        dialogButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null)
                    listener.onCompleted();
                MyDialog.this.dismiss();
            }
        });

        setOnCancelListener(new OnCancelListener() {

            @Override
            public void onCancel(DialogInterface dialog) {
                if(listener != null)
                    listener.onCanceled();
            }
        });
    }   public void setDialogListener(DialogListener listener) {
        this.listener = listener;
    }

}

并实施对话框:

    MyDialog dialog = new MyDialog(getContext(), title, text, button);
    dialog.setDialogListener(new MyDialog.DialogListener() {

        @Override
        public void onCompleted() {
            // do stuff when dialog is completed
        }

        @Override
        public void onCanceled() {
            // do stuff when dialog is cancelled
        }
    });
    dialog.show();
相关问题