如何修复“当我在漂亮的对话框按钮上单击两次时进行两次打印”

时间:2019-05-28 02:59:25

标签: java android

我调试了我的应用程序,然后尝试单击两次并快速单击漂亮的对话框按钮,它打印了两次,我希望即使我单击两次也不会打印两次。

我尝试了错误处理程序代码,但没有用。

prettyDialog.addButton("YES",
R.color.navy_blue,
R.color.gold_yellow,
new PrettyDialogCallback() {
    @Override
    public void onClick() {

        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {

                raw_serial = String.valueOf(Build.SERIAL);
                serial_final = raw_serial.substring(raw_serial.length() - 7);

                nf = new DecimalFormat("000000");

                DateFormat dateFormat = new SimpleDateFormat("MMddyyyy");
                Date date = new Date();
                ticket_date = dateFormat.format(date).toString();

                DateFormat datetimeformat = new SimpleDateFormat("HHmmss");
                Date time = new Date();

                ticket_time = datetimeformat.format(time).toString();
                transaction_no = serial_final + ticket_date + ticket_time + nf.format(setting_last_transaction_no);
                trace_no = merchant_code + serial_final + ticket_date + ticket_time + nf.format(ref_trace_no);
                print_card_no = Cardno.substring(Cardno.length() - 4);
                String print_balance;
                print_balance = "P" + String.valueOf(Balance);
                int balancespace = 0;
                int balancelength = 0;
                balancelength = print_balance.length();
                balancespace = 13 - balancelength;
                for (int j = 0; j < balancespace; j++) {
                    print_balance = " " + print_balance;
                }
                int print = PrinterInterface.open();
                Log.e("print", String.valueOf(print));
                int querystatus = PrinterInterface.queryStatus();
                Log.e("querystatus", String.valueOf(querystatus));
                DateFormat dateformatforprint = new SimpleDateFormat("MMM.dd,yyyy");
                Date dateformatprint = new Date();
                String datetoprint = dateformatforprint.format(dateformatprint).toString();
                DateFormat timeformattoprint = new SimpleDateFormat("HH:mm");
                Date timeformat = new Date();
                String timetoprint = timeformattoprint.format(timeformat).toString();
                String finalStation = "";
                Log.e("asdasd", "asdasd");
                if (print >= 0) {
                    byte[] arryTitle = null;
                    byte[] arryTrx = null;
                    byte[] arrySubtitle = null;
                    byte[] arryBody1 = null;
                    byte[] arryspace1 = null;
                    byte[] arryFooter = null;
                    //test
                    try {
                        arryTitle = ("\n" + "TRIPKO" + "\n" + "\n").getBytes("UTF-8");
                        arryTrx = ("TRX: " + transaction_no + "\n" +
                                trace_no + "\n").getBytes("UTF-8");
                        arrySubtitle = ("Date & Time  : " + datetoprint + " " + timetoprint + "\n" +
                                "Merchant Name: " + subcompany_name.toUpperCase() + "\n" +
                                "Branch       : " + terminal_name.toUpperCase() + "\n" +
                                "-------------------------------" + "\n" +
                                "\n").getBytes("UTF-8");
                        arryBody1 = ("Card No. : " + "  **** **** **** " + print_card_no + "\n" +
                                "Trans Type : " + "    " + type_name.toUpperCase() + "\n" +
                                "Current Balance  : " + print_balance + "\n" +
                                "-------------------------------" + "\n" +
                                "\n").getBytes("UTF-8");
                        arryFooter = ("Customer Service" + "\n"
                                + "Hotline#: TRIPKO(678-1234)" + "\n" +
                                "\n").getBytes("UTF-8");
                        arryspace1 = "\n".getBytes("UTF-8");
                    } catch (Throwable e) {

                        e.printStackTrace();
                    }
                    begin();
                    if (querystatus == 1) {

                        writeLineBreak(20);
                        write(arryspace1);
                        alignment(1);
                        Fontsize(16);
                        boldFont(2);
                        doublewidth();
                        write(arryTitle);
                        backtonormal();
                        nextline();

                        Fontsize(0);
                        write(arryTrx);
                        alignment(0);
                        write(arrySubtitle);
                        write(arryBody1);

                        alignment(1);
                        write(arryFooter);
                        write(arryspace1);
                        write(arryspace1);
                        write(arryspace1);
                        write(arryspace1);

                        insertData(company_id,
                                subcompany_id,
                                terminal_id,
                                setting_control_no,
                                Cardno,
                                Balance,
                                transaction_no,
                                trace_no,
                                device_serial);
                        setting_last_transaction_no += 1;
                        ref_trace_no += 1;
                        updateTicket(String.valueOf(setting_last_transaction_no),
                                String.valueOf(ref_trace_no));
                        DBBackup();
                        end();
                        btnCheckBalance.setEnabled(true);
                        btnCheckBalance.setClickable(true);
                        Intent intent2 = new Intent(getContext(), UpdateSendingServices.class);
                        getActivity().startService(intent2);
                    } else {
                        //OpenNoPaper();
                    }
                }
                PrinterInterface.close();
            }
        });
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                totalBalance.setText("P 0.00");
                btnCheckBalance.setEnabled(true);
                btnCheckBalance.setClickable(true);
            }
        });

        prettyDialog.dismiss();
    }
})
.show();

我希望即使单击“是”漂亮的对话框按钮也不会打印两次。

1 个答案:

答案 0 :(得分:0)

我认为您可以做这样的把戏

创建一个计数变量count=0;。在按钮单击中,验证条件,使if(count==0)显示对话框并进行count = 1。 (使用该对话框将不会再次打开),而关闭对话框时,请再次count = 0

我认为这会起作用

希望有帮助。

if(count == 0){
count = 1 ;
//showdialog process
} 

并且在dismiss()

之前
  ......
  count = 0; 
  prettyDialog.dismiss();