Android:在按钮上打开新布局点击并删除当前的

时间:2016-06-28 07:39:31

标签: c# android xamarin

我目前正在尝试熟悉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上的click事件被触发,我想用新的随机数再次打开相同的布局。我希望我能解释一下我的问题。

非常感谢提前

B中。南瓜

1 个答案:

答案 0 :(得分:2)

打开新的Finish后,您必须在当前Activity上致电button2.Click += (sender, e) => { var intent = new Intent(this, typeof(AIVsPlayerActivity)); StartActivity(intent); Finish(); };

#define DBHOST "tcp://127.0.0.1:3306"
#define USER "root"
#define PASSWORD " "
#define DATABASE "db_mmofps"

void CDBMySQL::Connect()
{
     m_driver = mysql::get_mysql_driver_instance();
     m_con = m_driver->connect(DBHOST, USER, PASSWORD);
     m_con->setSchema(DATABASE);
 }