In this third recipe, we will create a new activity responsible for displaying an equation and checking the answer.
We will now see how to solve the equations:
EquationActivity
and make it look like the following:namespace TheInternship { [Activity (Label = "EquationActivity")] public class EquationActivity : Activity { Dictionary<String, int> equations = new Dictionary<string, int> (); KeyValuePair<String, int> current_equation; protected override void OnCreate (Bundle bundle) { base.OnCreate (bundle); SetContentView (Resource.Layout.Equation); // Add some basic equations in our Dictionary of equation equations.Add ("2+2", 4); equations.Add ("4/2", 2); equations.Add ("3*3", 9); equations.Add ("3*25", 75); //Select one equation changeEquation (); // Check the response of the equation on click via a delegate Button button = FindViewById<Button> (Resource.Id.send_eq); button.Click += delegate { // Convert the response to int32. We could use a try/catch here. if(Convert.ToInt32(FindViewById<TextView> (Resource.Id.response).Text) == current_equation.Value) { Console.WriteLine ("Answer is OK..."); } else { Toast.MakeText (this, "Go to sleep!", ToastLength.Short).Show (); } }; } private void changeEquation() { //Load a random equation from the Dictionary Random rand = new Random (); current_equation = equations.ElementAt (rand.Next (0, equations.Count)); FindViewById<TextView> (Resource.Id.eq_text).Text = current_equation.Key; } } }
.axml
file under the Resources
/Layout
folder named Equation.axml
, and add the following code in order to construct a graphical interface with a TextView
, TextInput
, and button
elements:<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:textAppearance="?android:attr/textAppearanceLarge" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/eq_text" /> <EditText android:inputType="number" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/response" /> <Button android:text="OK" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/send_eq" /> </LinearLayout>
In the previous code, we use the OnCreate()
method of our newly created activity to create a list of equations and their answers:
Dictionary<String, int> equations = new Dictionary<string, int> (); equations.Add ("2+2", 4); equations.Add ("4/2", 2); equations.Add ("3*3", 9); equations.Add ("3*25", 75);
The Dictionary
argument belongs to the System.Collections.Generic
package and represents a list of keys and values, which fit our needs.
Then, we call the changeEquation()
method in which we randomly select one of the equations and populate the graphical interface with it:
Random rand = new Random (); current_equation = equations.ElementAt (rand.Next (0, equations.Count)); FindViewById<TextView> (Resource.Id.eq_text).Text = current_equation.Key;
Finally, we add a delegate
argument on the OK button and click and check whether the answer is correct:
Button button = FindViewById<Button> (Resource.Id.send_eq); button.Click += delegate { // Convert the response to int32. We could use a try/catch here. if(Convert.ToInt32(FindViewById<TextView> (Resource.Id.response).Text) == current_equation.Value) { Console.WriteLine ("Answer is OK..."); } else { Toast.MakeText (this, "Go to sleep!", ToastLength.Short).Show (); } };
3.145.105.105