开发者

avoid multiple clicks on a button

I have an application where in which i have two buttons, named 'next' and 'previous'. These buttons are to navigate through the stories being loaded from a url. When the button is tapped the dialog appears to show "Please wait loading data". When we tapp the next or p开发者_JAVA技巧revious button quite extravagantly, a never ending dialog appears on screen.

Am doing it this way:

public void onClick(View v) {

    try {
        pDialog = new ProgressDialog(StoriesListController.this);
        pDialog.setIndeterminate(true);
        pDialog.setIcon(R.drawable.icon_small);
        pDialog.setCancelable(true);
        if (isFavoriteList == true) {
            pDialog.setMessage("Loading favorites...");
        } else {
            pDialog.setMessage("Loading data...");
        }
        pDialog.setTitle("Please wait");
        pDialog.setOnCancelListener(new OnCancelListener() {

            public void onCancel(DialogInterface arg0) {
                if (cancelableHeavyWorker != null) {
                    cancelableHeavyWorker.setHandler(null);
                }
                finish();
            }
        });
        pDialog.show();

        if (v == next) {
            if (ind < items.size() - 1) {   
                ind++;
                loadAndShowNextActivity();
            }
        } else if (v == previous) {
            if (ind > 0) {
                ind--;
                loadAndShowNextActivity();
            }
        }

    } catch (Exception e) {

        Log.e("Error", "--.OnClink Message" + e.toString());
        e.printStackTrace();
    }
}

Where am i going wrong. How can i avoid multiple clicks or avoid never ending dialog. ANy help is appreciated.


Disable "next" button until the data is loaded completly or the process is canceled.


after clicking the button, do setEnabled(false) to this button, to disable multiple clicks


Alternatively to the other answers you could set a boolean variable to true when the button is clicked and set it to false when you're done processing the click.

This way you can ignore multiple clicks and not having to disable the button possibly avoiding annoying flickering of the button.


After click put the button setEnabled(false) and in the loadAndShowNextActivity() dismiss the pDialog.dismiss()


dialogButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
                dialogButton.setEnabled(false);
                ....



 dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialogInterface) {
                    dialogButton.setEnabled(true);
                }
            });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜