Beispiel #1
0
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.Main);

            if (savedInstanceState == null)     //if null, initialization
            {
                // Create the quote collection and load quotes
                quoteCollection = new QuoteBank();
                quoteCollection.LoadQuotes();
            }
            else       //not null, getting infromation from Bundle
            {
                // Deserialized the saved object state
                string        xmlQuoteCollection = savedInstanceState.GetString("Quotes");
                XmlSerializer x = new XmlSerializer(typeof(QuoteBank));
                quoteCollection = (QuoteBank)x.Deserialize(new StringReader(xmlQuoteCollection));
            }

            TextView quotationTextView = FindViewById <TextView>(Resource.Id.quoteTextView);

            quotationTextView.Text = quoteCollection.CurrentQuote.Quotation;
            DisplayScores();

            var whoEditText    = FindViewById <EditText>(Resource.Id.whoEditText);
            var answerTextView = FindViewById <TextView>(Resource.Id.answerTextView);

            // Display another quote when the "Next Quote" button is tapped
            var nextButton = FindViewById <Button>(Resource.Id.nextButton);

            nextButton.Click += delegate {
                quoteCollection.GetNextQuote();
                quotationTextView.Text = quoteCollection.CurrentQuote.Quotation;
            };

            // Enter button
            // Check the user's input with the answer and count scores
            var enterButton = FindViewById <Button>(Resource.Id.enterButton);

            enterButton.Click += delegate {
                if (!quoteCollection.CheckAnswer(whoEditText.Text))       // The answer is incorrect
                {
                    string person = quoteCollection.CurrentQuote.Person;
                    answerTextView.Text = "Wrong! The answer is " + person;

                    DisplayScores();
                    quoteCollection.GetNextQuote();
                    quotationTextView.Text = quoteCollection.CurrentQuote.Quotation;
                    whoEditText.Text       = "";
                }
                else
                {
                    answerTextView.Text = "Correct Answer!";                    // The answer is correct

                    DisplayScores();
                    quoteCollection.GetNextQuote();
                    quotationTextView.Text = quoteCollection.CurrentQuote.Quotation;
                    whoEditText.Text       = "";
                }
            };

            // Reset button
            // Set Scores as 0
            var resetButton = FindViewById <Button>(Resource.Id.resetButton);

            resetButton.Click += delegate
            {
                quoteCollection.ResetScores();
                DisplayScores();
            };
        }
Beispiel #2
0
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.Main);

            answerTextView = FindViewById <TextView>(Resource.Id.answerTextView);
            guessEditText  = FindViewById <EditText>(Resource.Id.guessEditText);
            scoreTextView  = FindViewById <TextView>(Resource.Id.scoreTextView);

            if (savedInstanceState != null)
            {
                //Deserialize the QuoteBank object
                string        currentQuotes = savedInstanceState.GetString("QuoteCollection");
                XmlSerializer x             = new XmlSerializer(typeof(QuoteBank));
                quoteCollection = (QuoteBank)x.Deserialize(new StringReader(currentQuotes));

                //get the score from the bundle and display
                right = savedInstanceState.GetInt("Right", 0);
                wrong = savedInstanceState.GetInt("Wrong", 0);
                SetScore();
            }
            else
            {   // Create the quote collection and display the current quote
                quoteCollection = new QuoteBank();
                quoteCollection.LoadQuotes();
                quoteCollection.GetNextQuote();
            }

            quotationTextView      = FindViewById <TextView>(Resource.Id.quoteTextView);
            quotationTextView.Text = quoteCollection.CurrentQuote.Quotation;



            // Display another quote when the "Next Quote" button is tapped
            var nextButton = FindViewById <Button>(Resource.Id.nextButton);

            nextButton.Click += delegate {
                //clear the previous guess and answer
                guessEditText.Text  = "";
                answerTextView.Text = "";

                quoteCollection.GetNextQuote();
                quotationTextView.Text = quoteCollection.CurrentQuote.Quotation;
            };

            var enterButton = FindViewById <Button>(Resource.Id.enterButton);

            enterButton.Click += delegate {
                guess = guessEditText.Text;

                //check to see if correct answer
                Boolean answer = quoteCollection.CheckAnswer(quoteCollection.CurrentQuote, guess);

                //if correct
                if (answer)
                {
                    answerTextView.Text = GetString(Resource.String.CorrectAnswer);
                    right = quoteCollection.Right;
                    SetScore();
                }
                //if not correct
                else
                {
                    wrong = quoteCollection.Wrong;
                    SetScore();
                    person = quoteCollection.CurrentQuote.Person;
                    answerTextView.Text = GetString(Resource.String.IncorrectAnswer) + person;
                }
            };

            var restartButton = FindViewById <Button>(Resource.Id.restartButton);

            restartButton.Click += delegate
            {
                guessEditText.Text  = "";
                answerTextView.Text = "";
                scoreTextView.Text  = "";

                right = 0;
                wrong = 0;

                quoteCollection = new QuoteBank();
                quoteCollection.LoadQuotes();
                quoteCollection.GetNextQuote();
                quotationTextView.Text = quoteCollection.CurrentQuote.Quotation;
            };
        }