MainActivity.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System;
  2. using Android.App;
  3. using Android.Content;
  4. using Android.OS;
  5. using Android.Widget;
  6. namespace CreditCardValidation.Droid
  7. {
  8. [Activity(Label = "@string/app_name", MainLauncher = true, Icon = "@drawable/icon", Theme = "@android:style/Theme.Holo.Light")]
  9. public class MainActivity : Activity
  10. {
  11. EditText _creditCardTextField;
  12. TextView _errorMessagesTextField;
  13. Button _validateButton;
  14. protected override void OnCreate(Bundle bundle)
  15. {
  16. base.OnCreate(bundle);
  17. // Set our view from the "main" layout resource
  18. SetContentView(Resource.Layout.Main);
  19. _errorMessagesTextField = FindViewById<TextView>(Resource.Id.errorMessagesText);
  20. _creditCardTextField = FindViewById<EditText>(Resource.Id.creditCardNumberText);
  21. _validateButton = FindViewById<Button>(Resource.Id.validateButton);
  22. _validateButton.Click += (sender, e) =>{
  23. _errorMessagesTextField.Text = String.Empty;
  24. string errMessage;
  25. if (IsCCValid(out errMessage))
  26. {
  27. Intent i = new Intent(this, typeof(CreditCardValidationSuccess));
  28. StartActivity(i);
  29. }
  30. else
  31. {
  32. RunOnUiThread(() => { _errorMessagesTextField.Text = errMessage; });
  33. }
  34. };
  35. }
  36. bool IsCCValid(out string errMessage)
  37. {
  38. errMessage = "";
  39. if (_creditCardTextField.Text.Length < 16)
  40. {
  41. errMessage = "Credit card number is to short.";
  42. return false;
  43. }
  44. if (_creditCardTextField.Text.Length > 16)
  45. {
  46. errMessage = "Credit card number is to long.";
  47. return false;
  48. }
  49. return true;
  50. }
  51. }
  52. }