按钮单击以切换

时间:2014-08-18 08:02:51

标签: c# android xamarin

任何人都可以帮助我将此序列转换为Switch。我尝试了但是我的所有尝试都失败了。

private void ButtonTouch ( EditText x)
{
    if (i==2) 
    {
        Button1.Click += delegate {
            x.Text = x.Text + "1";
        };
        Button2.Click += delegate {
            x.Text = x.Text + "2";
        };
        Button3.Click += delegate {
            x.Text = x.Text + "3";
        };
        Button4.Click += delegate {
            x.Text = x.Text + "4";
        };
        Button5.Click += delegate {
            x.Text = x.Text + "5";
        };
        Button6.Click += delegate {
            x.Text = x.Text + "6";
        };
        Button7.Click += delegate {
            x.Text = x.Text + "7";
        };
        Button8.Click += delegate {
            x.Text = x.Text + "8";
        };
        Button9.Click += delegate {
            x.Text = x.Text + "9";
        };
        Button0.Click += delegate {
            x.Text = x.Text + "0";
        };
    }


    a.Click += delegate {
        i=0;
        i = i + 1;
        this.ButtonTouch (a);
    };
    b.Click += delegate {
        i=0;
        i = i + 1;
        this.ButtonTouch (b);
    };
    c.Click += delegate {
        i=0;
        i = i + 1;
        this.ButtonTouch (c);
    };
}

尝试:

switch (Button)
{
case 'Button1.Click':....... ; break;
.......
.......
}

switch (true)
{
case 'Button1':........; break;
........
........
}

我没有想法......相关的想法。所以,如果有人打扰帮助我,我感激不尽。提前谢谢。

2 个答案:

答案 0 :(得分:0)

你可以尝试

for(int i=0; i<=9; i++)
{
    Button button = (Button)YourPageControl.FindName("Button" + i);
    button.Click += delegate 
                    {
                         x.Text = x.Text + i;
                    };
}

答案 1 :(得分:0)

试试这个,但我不认为这会解决你的问题,你的问题必须是别的东西:

if (i==2) 
{
var ListButtons = new List<Button> {
{ Button1 },
{ Button2 },
{ Button3 },
{ Button4 },
{ Button5 },
{ Button6 },
{ Button7 },
{ Button8 },
{ Button9 },
{ Button0 },
};
for(int i=0; i<=9; i++)
{
switch (i)
{
 case 0:
{
    ListButtons[i].Click += delegate {
            x.Text = x.Text + i;
        };
    break;
}
case 1:
{
    ListButtons[i].Click += delegate {
            x.Text = x.Text + i;
        };
    break;
}
case 2:
{
    ListButtons[i].Click += delegate {
            x.Text = x.Text + i;
        };
    break;
}
case 3:
{
    ListButtons[i].Click += delegate {
            x.Text = x.Text + i;
        };
    break;
}
case 4:
{
    ListButtons[i].Click += delegate {
            x.Text = x.Text + i;
        };
    break;
}
case 5:
{
    ListButtons[i].Click += delegate {
            x.Text = x.Text + i;
        };
    break;
}
case 6:
{
    ListButtons[i].Click += delegate {
            x.Text = x.Text + i;
        };
    break;
}
case 7:
{
    ListButtons[i].Click += delegate {
            x.Text = x.Text + i;
        };
    break;
}
case 8:
{
    ListButtons[i].Click += delegate {
            x.Text = x.Text + i;
        };
    break;
}
case 9:
{
    ListButtons[i].Click += delegate {
            x.Text = x.Text + i;
        };
    break;
}
}
}
}