gpt4 book ai didi

c# - Android:在按钮上打开新布局单击并删除当前布局

转载 作者:搜寻专家 更新时间:2023-11-01 09:46:01 24 4
gpt4 key购买 nike

我目前正在尝试熟悉 Xamarin,因此我创建了一个小应用程序。在此应用程序中,我想单击一个打开新布局的按钮。但是当我在新布局中并按下手机上的后退按钮时,它会返回到以前的布局。我不想要那个。因为我要打开多个相同布局的实例,但我不想将这些实例保存在历史记录中。这是我的代码

public class AIVsPlayerActivity : Activity
{
private int minValue = 0;
private int maxValue = 100;
private int valueToGuess;
private int tries = 0;

private bool hasWon = false;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);

// Set View
SetContentView(Resource.Layout.AIVsPlayer);

// Set value which has to be guessed
var random = new Random();
valueToGuess = random.Next(minValue, maxValue);

// Create your application here

TextView result = FindViewById<TextView>(Resource.Id.labResult);
TextView labTries = FindViewById<TextView>(Resource.Id.labTries);
Button button1 = FindViewById<Button>(Resource.Id.btnGuess);
Button button2 = FindViewById<Button>(Resource.Id.btnNewRound);

button1.Click += (sender, e) =>
{
var guess = FindViewById<EditText>(Resource.Id.tbGuess);
int numberGuessed = int.Parse(guess.Text);

if (numberGuessed > valueToGuess)
{
result.Text = "Your number is too high";
tries++;
}
else if (numberGuessed < valueToGuess)
{
result.Text = "Your number is too low";
tries++;
}
else if (numberGuessed == valueToGuess)
{
result.Text = "You made it! You've guessed my number!";
//Console.WriteLine("{0} tries were needed", tries);
hasWon = true;
}
labTries.Text = tries.ToString();

if (hasWon)
{
button2.Visibility = ViewStates.Visible;
button1.Visibility = ViewStates.Invisible;
}
};

button2.Click += (sender, e) =>
{
var intent = new Intent(this, typeof(AIVsPlayerActivity));
StartActivity(intent);
};

}
}

如果 button2 上的点击事件被触发,我想打开相同的布局,只是用一个新的随机数再次清空。我希望我能解释我的问题。

提前致谢

B.南瓜

最佳答案

打开新的 Activity 后,您必须调用 Finish:

button2.Click += (sender, e) =>
{
var intent = new Intent(this, typeof(AIVsPlayerActivity));
StartActivity(intent);
Finish();
};

关于c# - Android:在按钮上打开新布局单击并删除当前布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38069978/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com