本文介绍了从收盘正面按钮,单击停止AlertDialog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想建立一个自定义AlertDialog,其中有2个按钮,取消和积极的按钮。我需要它,因此积极的按钮被点击了什么,我可以改变文本,不具备对话密切。

粗流是正的按钮将显示发送,当被点击它,它会变为正在发送...,那么code将一些数据发送到我们​​的服务器,如果反应是真实的,关闭对话框,如果是假的,或者超时等显示错误信息(面包),并保持对话打开。

我有code发送数据到服务器,处理响应等,我只是想不出如何编辑AlertDialog类实现这一点。有谁知道我怎么会去这样做?

电流测试code:

  AlertDialog.Builder B =新AlertDialog.Builder(getActivity());
b.setView(getActivity()getLayoutInflater()膨胀(R.layout.dialog_single_text,NULL));
b.setTitle(忘记密码);
b.setMessage(请输入你的电子邮件);
b.setPositiveButton(发送,新DialogInterface.OnClickListener(){
    @覆盖
    公共无效的onClick(DialogInterface对话,诠释它){
        Toast.makeText(getActivity()发送...,Toast.LENGTH_SHORT).show();
    }
});
。b.create()显示();


解决方案

您可以将onShowListener添加到AlertDialog

  d.setOnShowListener(新DialogInterface.OnShowListener(){    @覆盖
    公共无效昂秀(DialogInterface对话){        按钮B = d.getButton(AlertDialog.BUTTON_POSITIVE);
        b.setOnClickListener(新View.OnClickListener(){            @覆盖
            公共无效的onClick(查看视图){
                // TODO做点什么                //辞退一旦一切都OK。
                d.dismiss();
            }
        });
    }
});

I'm trying to setup a custom AlertDialog, which has 2 buttons, cancel and a positive button. I need to make it so what the positive button is clicked, I can change the text, and not have the dialog close.

Rough flow is the positive button will say "Send", when it's clicked it will change to "Sending...", then the code will send some data to our server, and if the response is true, close the dialog, if it's false, or timeouts etc show an error message (Toast) and keep the dialog open.

I have code for sending data to the server, handling responses etc, I just can't think how to edit the AlertDialog class implement this. Does anyone know how I'd go about doing this?

Current testing code:

AlertDialog.Builder b = new AlertDialog.Builder(getActivity());
b.setView(getActivity().getLayoutInflater().inflate(R.layout.dialog_single_text, null));
b.setTitle("Forgotten Password");
b.setMessage("Please enter your email");
b.setPositiveButton("Send", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        Toast.makeText(getActivity(), "Sending...", Toast.LENGTH_SHORT).show();
    }
});
b.create().show();
解决方案

you can add an onShowListener to the AlertDialog

 d.setOnShowListener(new DialogInterface.OnShowListener() {

    @Override
    public void onShow(DialogInterface dialog) {

        Button b = d.getButton(AlertDialog.BUTTON_POSITIVE);
        b.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                // TODO Do something

                //Dismiss once everything is OK.
                d.dismiss();
            }
        });
    }
});

这篇关于从收盘正面按钮,单击停止AlertDialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 17:38