Prevent quick clicks on multiple controls?

I have encountered a problem to prevent quick clicking on multiple controls in the xamarin.forms PCL project on Android. Basically, I need to solve the following four situations:

>Click buttons and back buttons in the NavigationBar > click buttons and hardware back buttons > Click two or more buttons at the same time > click the same button multiple times

I use navigationpage with 3 pages - mainpage, secondpage and thirdpage. Each page has buttons to handle navigation

MainPage.xaml.cs

    public async void NavigateToSecondPage(object s, EventArgs e)
    {
        await Navigation.PushAsync(new SecondPage());
    }

SecondPage.xaml.cs

    public async void NavigateToMainPage(object s, EventArgs e)
    {
        if(await DisplayAlert("Alert", "Are you really sure you want to navigate back?", "yes", "no"))
            await Navigation.PopAsync();
    }

    public async void NavigateToThirdPage(object s, EventArgs e)
    {
        await Navigation.PushAsync(new ThirdPage());
    }

ThirdPage.xaml.cs

    public async void NavigateToSecondPage(object s, EventArgs e)
    {
        await Navigation.PopAsync();
    }

The first and second situations cause this problem, that is, from the second page "navigatetomainpage" event handler Message@R_ 910_ 2419 @ on mainpage. The third case is similar, but in secondpage Message@R_ 910_ 2419 @ on thirdpage. The last case causes the same page to be stacked multiple times on the navigationstack

Are there solutions that cover all of these situations? This problem also applies to local Android developers

Solution replicating issue

Invalid Sign

resolvent:

You can use the global Boolean "canclick". By default, it is set to true. When clicking, check whether it is false. If it is false, no operation will be performed (another click is being processed); If true, set it to false, start the normal operation on the button, and then set it to true again after completion (to handle future clicks)

When executing an asynchronous method, you may have to set the variables public and static to access it from the "firstpage" "secondpage"... Object so that it can be set to false when loaded correctly

Alternative methods include two custom functions "allowclicks()" and "forbidclicks()". Disable all buttons on forbidclicks and enable them in allowclicks. When the user clicks the button, call "disable", and then allow the button after processing. Depending on your treatment, you may have to make these functions static (this may not be possible)

I hope it helps

Editing: code examples

public static void disableButtons() {
    button1.setEnabled(false);
    button2.setEnabled(false);
    button3.setEnabled(false);
    button4.setEnabled(false);
}
public static void enableButtons() {
    button1.setEnabled(true);
    button2.setEnabled(true);
    button3.setEnabled(true);
    button4.setEnabled(true);
}

Then call yourClass.disableButtons (); Click on each button. Then "yourclass. Enablebuttons();" after the treatment is completed. I can't say exactly, because I don't know whether you use Android completely (then, you have an activity somewhere), whether the "page" is an activity or a fragment... And so on

The rest of the code depends on this. If each page is an activity, I'm almost sure it will be more complex because UI changes to suspended activities are unsafe. If pages are fragments, you may have to use interfaces and fragment functions instead of static functions

You're not precise enough about what you're doing, so I can't be more precise in the code

However, let's try as I said first. Suppose you have only one activity and each page is the "layout" (not fragment) of the activity. The code will be like this:

private boolean canClick=true;

In oncreate():

button1.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View view) {
        if(canClick) {
            canClick=false;
            /*show your other page here
            if showing is ascync, canClick=true; must be done AFTER showing, use services, handlers, 
            here i assume nothing is async, so i put canClick=true; directly after treatment
            */
            canClick=true;
        }
    }
});

Then, you can customize each processing method as needed by using the same code for each button. You can also replace it with "canclick = false;" through "canclick = true" of "disablebuttons();" and "enclickbuttons();". If you use the above code, delete the if statement

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>