آموزش dialog اندروید در برنامه نویسی زامارین

3 سال پیش

آموزش dialog اندروید در برنامه نویسی زامارین

در این درس از آموزش های برنامه نویسی سایت سورس باران، با آموزش dialog اندروید در برنامه نویسی زامارین در خدمت شما هستیم.

 

Alert Dialog

در این بخش، ما می خواهیم یک دکمه ایجاد کنیم که با کلیک کردن یک کادر alert dialog نمایش داده می شود. کادر گفتگو شامل دو دکمه است، به عنوان مثال، دکمه های Delete و Cancel.

اول از همه، به main.axml بروید و یک دکمه جدید در داخل طرح بندی خطی ایجاد کنید همانطور که در کد زیر نشان داده شده است.

<?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:background = "#d3d3d3" 
   android:layout_height = "fill_parent"> 
   <Button 
      android:id="@+id/MyButton" 
      android:layout_width = "fill_parent" 
      android:layout_height = "wrap_content" 
      android:text = "Click to Delete" 
      android:textColor = "@android:color/background_dark" 
      android:background = "@android:color/holo_green_dark" /> 
</LinearLayout>

 

در ادامه، MainActivity.cs را باز کنید تا

protected override void OnCreate(Bundle bundle) { 
   base.OnCreate(bundle); 
   SetContentView(Resource.Layout.Main); 
   Button button = FindViewById<Button>(Resource.Id.MyButton); 
   button.Click += delegate { 
      AlertDialog.Builder alertDiag = new AlertDialog.Builder(this); 
      alertDiag.SetTitle("Confirm delete"); 
      alertDiag.SetMessage("Once deleted the move cannot be undone"); 
      alertDiag.SetPositiveButton("Delete", (senderAlert, args) => { 
         Toast.MakeText(this, "Deleted", ToastLength.Short).Show();
      }); 
      alertDiag.SetNegativeButton("Cancel", (senderAlert, args) => { 
         alertDiag.Dispose(); 
      }); 
      Dialog diag = alertDiag.Create(); 
      diag.Show(); 
   }; 
}

ایجاد شود و عملکرد آن اضافه شود.

protected override void OnCreate(Bundle bundle) { 
   base.OnCreate(bundle); 
   SetContentView(Resource.Layout.Main); 
   Button button = FindViewById<Button>(Resource.Id.MyButton); 
   button.Click += delegate { 
      AlertDialog.Builder alertDiag = new AlertDialog.Builder(this); 
      alertDiag.SetTitle("Confirm delete"); 
      alertDiag.SetMessage("Once deleted the move cannot be undone"); 
      alertDiag.SetPositiveButton("Delete", (senderAlert, args) => { 
         Toast.MakeText(this, "Deleted", ToastLength.Short).Show();
      }); 
      alertDiag.SetNegativeButton("Cancel", (senderAlert, args) => { 
         alertDiag.Dispose(); 
      }); 
      Dialog diag = alertDiag.Create(); 
      diag.Show(); 
   }; 
}

 

پس از اتمام، برنامه خود را بسازید و اجرا کنید تا نتیجه را مشاهده کنید.

Confirm Delete

 

در کد بالا، ما یک alert dialog به نام alertDiag با دو دکمه زیر ایجاد کرده ایم –

  • setPositiveButton – شامل  دکمه Delete است که با کلیک روی آن confirmation message Deleted نمایش داده می شود.
  • setNegativeButton – این شامل یک دکمه Cancel است که با کلیک بر روی آن کادر alert dialog بسته می شود.

 

منبع.

 

لیست جلسات قبل آموزش برنامه نویسی زامارین

  1. آموزش برنامه نویسی زامارین (Xamarin)
  2. آموزش نصب زامارین
  3. نوشتن اولین برنامه در برنامه نویسی زامارین 
  4. آموزش فایل مانیفست در برنامه نویسی زامارین
  5. منابع اندروید در برنامه نویسی زامارین
  6. چرخه فعالیت اندروید در برنامه نویسی زامارین
  7. مجوزها در برنامه نویسی زامارین
  8. آموزش ساخت برنامه رابط کاربری گرافیکی در برنامه نویسی زامارین
  9. آموزش منوها در برنامه نویسی زامارین
  10. آموزش طرح بندی در برنامه نویسی زامارین
  11. آموزش ویجت های اندروید در برنامه نویسی زامارین
0
برچسب ها :
نویسنده مطلب erfan molaei

دیدگاه شما

بدون دیدگاه