我正在尝试构建一个 for 函数来创建多个带有随机案例的按钮。
我不知道如何使用我的变量来创建这些按钮?
这是我的代码的一部分:
位置字符串用于确定我正在创建哪个按钮。showButton 字符串用于确定我正在使用的按钮 ID。
string showButton, position;
for(int i = 1; i<=12; i++)
{
showButton = "imagebutton" + i;
position = "position" + i;
Random random = new Random();
int randomNum = random.Next(1,4);
switch (randomNum)
{
case 1:
ImageButton position = FindViewById<ImageButton>(Resource.Id.showButton);
position1.SetImageResource(Resource.Drawable.buttonGreen);
btnPosition.Click += (o, e) =>
{
Toast.MakeText(this, "Você venceu", ToastLength.Short).Show();
};
break;
case 2:
SetContentView(Resource.Layout.Main);
ImageButton position = FindViewById<ImageButton>(Resource.Id.showButton);
position.SetImageResource(Resource.Drawable.buttonYellow);
position.Click += (o, e) =>
{
Toast.MakeText(this, "Você Venceu", ToastLength.Short).Show();
};
break;
case 3:
SetContentView(Resource.Layout.Main);
ImageButton position = FindViewById<ImageButton>(Resource.Id.showButton);
position.SetImageResource(Resource.Drawable.buttonRed);
position.Click += (o, e) =>
{
Toast.MakeText(this, "Você Perdeu", ToastLength.Short).Show();
};
break;
}