android一键单击两次自动

时间:2018-09-08 04:45:23

标签: android

我创建了一个使用打印机无线连接的应用程序并进行打印。当我单击按钮时,它将仅打印1次。单击后我想再打印一次。因此,这意味着当我单击1次5秒后,此按钮将自动单击,而无需再次单击。在我的代码下面,它正在工作,但不停地。我希望经过2次处理后它将停止线程。您有什么解决方案请帮忙。谢谢。下面是我的代码:

public void buttonPrintClicked(){
    buttonPrint.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            buttonPrintGotClick();
        }
    });
}

// Do something when button print was click
private void buttonPrintGotClick(){

    int noOfSecond = 5;
    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            //TODO Set button auto perform click.
            buttonPrint.performClick();

            // Close current Activity
            get_order_Activity.this.finish();
        }
    }, noOfSecond * 500);

    // Delete Previous PDF file first before create new again
    deleteFileFromStorage();

    progressDialog = new ProgressDialog(get_order_Activity.this);
    progressDialog.setMessage("Please wait");
    bitmap = loadBitmapFromView(linearLayout_PDF, linearLayout_PDF.getWidth(), linearLayout_PDF.getHeight());
    createPdf();

    // Open File from Storage
    openFileFromStorage();
}

1 个答案:

答案 0 :(得分:1)

我认为您需要将postDelayed放在onClick回调中。这是我编辑的代码。

public void buttonPrintClicked(){
    buttonPrint.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            buttonPrintGotClick();
            int noOfSecond = 5;
            new Handler().postDelayed(new Runnable() {

                @Override
                public void run() {
                    buttonPrintGotClick();
                    get_order_Activity.this.finish();
                }
            }, noOfSecond * 500);

        }
    });
}

// Do something when button print was click
private void buttonPrintGotClick(){
    // Delete Previous PDF file first before create new again
    deleteFileFromStorage();

    progressDialog = new ProgressDialog(get_order_Activity.this);
    progressDialog.setMessage("Please wait");
    bitmap = loadBitmapFromView(linearLayout_PDF, linearLayout_PDF.getWidth(), linearLayout_PDF.getHeight());
    createPdf();
    // Open File from Storage
    openFileFromStorage();
}