Advertisement
MagnusArias

PUM | Zad 1

Mar 9th, 2017
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.81 KB | None | 0 0
  1. using Android.App;
  2. using Android.Widget;
  3. using Android.OS;
  4. using System;
  5. using static System.Text.RegularExpressions.Regex;
  6. using Android.Content;
  7.  
  8. namespace Zad1PUM
  9. {
  10.     [Activity(Label = "Zad1PUM", MainLauncher = true, Icon = "@drawable/icon")]
  11.     public class MainActivity : Activity
  12.     {
  13.         private EditText phoneNumber;
  14.         private Button callButton;
  15.         private EditText phoneNumberTrans;
  16.         private Button translateButton;
  17.  
  18.         protected override void OnCreate(Bundle bundle)
  19.         {
  20.             base.OnCreate(bundle);
  21.  
  22.             // Set our view from the "main" layout resource
  23.             SetContentView (Resource.Layout.Main);
  24.  
  25.             translateButton =   FindViewById<Button>(Resource.Id.Translate);
  26.             callButton =        FindViewById<Button>(Resource.Id.ButtonCall);
  27.             phoneNumber =       FindViewById<EditText>(Resource.Id.PhoneNumber);
  28.             phoneNumberTrans =  FindViewById<EditText>(Resource.Id.PhoneNumberTranslated);
  29.  
  30.             translateButton.Click += (object sender, EventArgs e) =>
  31.             {
  32.                 string number = phoneNumber.Text;
  33.  
  34.                 number = Replace(number, @"[abcABC]", "2");
  35.                 number = Replace(number, @"[defDEF]", "3");
  36.                 number = Replace(number, @"[ghiGHI]", "4");
  37.                 number = Replace(number, @"[jklJKL]", "5");
  38.                 number = Replace(number, @"[mnoMNO]", "6");
  39.                 number = Replace(number, @"[spqrSPQR]", "7");
  40.                 number = Replace(number, @"[tuvTUV]", "8");
  41.                 number = Replace(number, @"[wxyzWXYZ]", "9");
  42.  
  43.                 phoneNumberTrans.SetText(number, TextView.BufferType.Editable);
  44.                  
  45.             };
  46.  
  47.             callButton.Click += (object sender, EventArgs e) =>
  48.             {
  49.                 var callIntent = new Intent(Intent.ActionCall);
  50.                 callIntent.SetData(Android.Net.Uri.Parse("tel:" + phoneNumberTrans.Text));
  51.                 StartActivity(callIntent);
  52.             };
  53.        
  54.         }
  55.  
  56.  
  57.     }
  58.  
  59. }
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66. using Android.App;
  67. using Android.Widget;
  68. using Android.OS;
  69. using System;
  70. using static System.Text.RegularExpressions.Regex;
  71. using Android.Content;
  72.  
  73. namespace Zad1PUM
  74. {
  75.     [Activity(Label = "Zad1PUM", MainLauncher = true, Icon = "@drawable/icon")]
  76.     public class MainActivity : Activity
  77.     {
  78.         private EditText phoneNumber;
  79.         private EditText message;
  80.         private Button translateButton;
  81.  
  82.         protected override void OnCreate(Bundle bundle)
  83.         {
  84.             base.OnCreate(bundle);
  85.  
  86.             // Set our view from the "main" layout resource
  87.             SetContentView (Resource.Layout.Main);
  88.  
  89.             translateButton =   FindViewById<Button>(Resource.Id.Translate);
  90.             phoneNumber =       FindViewById<EditText>(Resource.Id.PhoneNumber);
  91.             message =           FindViewById<EditText>(Resource.Id.Message);
  92.  
  93.  
  94.             translateButton.Click += (object sender, EventArgs e) =>
  95.             {
  96.                 int n;
  97.                 bool isNumeric = int.TryParse(phoneNumber.Text, out n);
  98.  
  99.                 if (isNumeric && String.IsNullOrWhiteSpace(message.Text))
  100.                 {
  101.                     var callIntent = new Intent(Intent.ActionCall);
  102.                     callIntent.SetData(Android.Net.Uri.Parse("tel:" + phoneNumber.Text));
  103.                     StartActivity(callIntent);
  104.                 }
  105.                 else if (isNumeric && !String.IsNullOrWhiteSpace(message.Text))
  106.                 {
  107.                     var smsUri = Android.Net.Uri.Parse("smsto:" + phoneNumber.Text);
  108.                     var smsIntent = new Intent(Intent.ActionSendto, smsUri);
  109.                     smsIntent.PutExtra("sms_body", message.Text);
  110.                     StartActivity(smsIntent);
  111.                 }
  112.                 else if (!isNumeric && String.IsNullOrWhiteSpace(message.Text))
  113.                 {
  114.                     Toast.MakeText(this, "Nie podano żadnych danych", ToastLength.Long).Show();
  115.                 }
  116.                 else
  117.                 {
  118.                     try {                
  119.                         var email = new Intent(Android.Content.Intent.ActionSend);
  120.                         email.PutExtra(Android.Content.Intent.ExtraEmail, phoneNumber.Text );
  121.  
  122.                         email.PutExtra(Android.Content.Intent.ExtraText, message.Text);
  123.                         email.SetType("message/rfc822");
  124.                    
  125.                         StartActivity(email);
  126.  
  127.                     } catch (Android.Content.ActivityNotFoundException)
  128.                     {
  129.                         Toast.MakeText(this, "Wystapił błąd podczas wysyłania wiadomości", ToastLength.Long).Show();
  130.                     }
  131.                 }
  132.             };  
  133.        
  134.         }
  135.  
  136.     }
  137.  
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement