One click Event on many buttons

Hi, I'm new at Android Apps and Xamarin. Can somebody show me how can I make a code that will allow to one click on only one of few buttons.
Can I use switch case somehow? I was searching for the solution, but I didn't find anything that helped me.
Can you maybe do example on this simple code?

using Android.App;
using Android.Widget;
using Android.OS;

namespace Try1
{
[Activity (Label = "Try1", MainLauncher = true, Icon = "@mipmap/icon")]
public class MainActivity : Activity
{
int count = 1;

    protected override void OnCreate (Bundle savedInstanceState)
    {
        Xamarin.Insights.Initialize (XamarinInsights.ApiKey, this);
        base.OnCreate (savedInstanceState);
        // Set our view from the "main" layout resource
        SetContentView (Resource.Layout.Main);
        // Get our button from the layout resource,
        // and attach an event to it
        Button button = FindViewById<Button> (Resource.Id.myButton);
        Button button10 = FindViewById<Button> (Resource.Id.button10);
        Button button11 = FindViewById<Button> (Resource.Id.button11);
        Button button12 = FindViewById<Button> (Resource.Id.button12);

        button.Click += delegate {
            button.SetBackgroundColor(Android.Graphics.Color.Yellow);
        };
        button10.Click += delegate {
            button10.SetBackgroundColor(Android.Graphics.Color.Yellow);
        };
        button11.Click += delegate {
            button11.SetBackgroundColor(Android.Graphics.Color.Yellow);
        };
        button12.Click += delegate {
            button12.SetBackgroundColor(Android.Graphics.Color.Yellow);
        };
    }
}

}

Tagged:

Posts

  • Maybe put your buttons in a List and iterate on that list?

  • none555666none555666 PLMember
    edited January 2016

    When I use a list there is still the same problem - the program allow to click more than one button. I want to have an Event that will check if one of the Buttons was clicked and than it will return the id of the button clicked (and then do something according to which button was it). Can I check somehow if any of the button from the list was clicked and if yes, how can I do this?

Sign In or Register to comment.