xamarin android alertdialog详解

来源:互联网 发布:淘宝首页轮播图怎么来 编辑:程序博客网 时间:2024/05/20 13:36

说明一下:学习xamarin Android一段时间,准备写一些xamarin android相关的例子,alertdialog也是使用的非常多得空间之一,非常感谢鸟巢上的小猪,我也是看着他写的教程学会的。参考他的那一章 http://www.runoob.com/w3cnote/android-tutorial-alertdialog.html

1.基本使用流程

  • Step 1:创建AlertDialog.Builder对象;
  • Step 2:调用setIcon()设置图标,setTitle()setCustomTitle()设置标题;
  • Step 3:设置对话框的内容:setMessage()还有其他方法来指定显示的内容;
  • Step 4:调用setPositive/Negative/NeutralButton()设置:确定,取消,中立按钮;
  • Step 5:调用create()方法创建这个对象,再调用show()方法将对话框显示出来;

2.几种常用的对话框使用示例

运行效果图:

 

主要代码:MainActivity.class

[html] view plain copy
print?
  1. public class MainActivity : Activity  
  2.     {  
  3.         int count = 1;  
  4.         private Button btn_alertDialog_one, btn_alertDialog_two, btn_alertDialog_three, btn_alertDialog_four;  
  5.   
  6.         private bool[] checkItems;  
  7.         private AlertDialog alertDialog = null;  
  8.         private AlertDialog.Builder builder = null;  
  9.         protected override void OnCreate(Bundle bundle)  
  10.         {  
  11.             base.OnCreate(bundle);  
  12.             SetContentView(Resource.Layout.Main);  
  13.              bindViewClick();  
  14.         }  
  15.   
  16.         private void bindViewClick()  
  17.         {  
  18.             btn_alertDialog_four = FindViewById<Button>(Resource.Id.btn_alertDialog_four);  
  19.             btn_alertDialog_three = FindViewById<Button>(Resource.Id.btn_alertDialog_three);  
  20.             btn_alertDialog_two = FindViewById<Button>(Resource.Id.btn_alertDialog_two);  
  21.             btn_alertDialog_one = FindViewById<Button>(Resource.Id.btn_alertDialog_one);  
  22.   
  23.             btn_alertDialog_one.Click += delegate { onClick(btn_alertDialog_one); };  
  24.             btn_alertDialog_two.Click += delegate { onClick(btn_alertDialog_two); };  
  25.             btn_alertDialog_three.Click += delegate { onClick(btn_alertDialog_three); };  
  26.             btn_alertDialog_four.Click += delegate { onClick(btn_alertDialog_four); };  
  27.         }  
  28.         private void onClick(View v)  
  29.         {  
  30.             switch (v.Id)  
  31.             {  
  32.                 case Resource.Id.btn_alertDialog_one:  
  33.                     alertDialog = null;  
  34.                     builder = new AlertDialog.Builder(this);  
  35.                     alertDialog = builder  
  36.                    .SetTitle("sure")  
  37.                    .SetMessage("are you sure exit?")  
  38.                    .SetNegativeButton("cancel", (s, e) =>  
  39.                     {  
  40.                         Toast.MakeText(this, "you click cancel", ToastLength.Short).Show();  
  41.                     })  
  42.                    .SetPositiveButton("sure", (s, e) =>  
  43.                    {  
  44.                        Toast.MakeText(this, "you click sure", ToastLength.Short).Show();  
  45.                    })  
  46.                    .SetNeutralButton("neutra", (s, e) => {  
  47.                        Toast.MakeText(this, "you click neutra", ToastLength.Short).Show();  
  48.                    })  
  49.                    .Create();       //创建alertDialog对象  
  50.               
  51.                    alertDialog.Show();  
  52.                     var dialog = new AlertDialog.Builder(this);  
  53.                     break;  
  54.   
  55.                 case Resource.Id.btn_alertDialog_two:  
  56.                     alertDialog = null;  
  57.                     builder = new AlertDialog.Builder(this);  
  58.                     string[] players = new string[] {"杜兰特","汤普森","考辛斯","卡戴珊"};  
  59.                     alertDialog = builder  
  60.                         .SetIcon(Resource.Drawable.players)  
  61.                         .SetTitle("选择你喜欢的球员")  
  62.                          .SetItems(players, (s, e) =>  
  63.                          {  
  64.                              Toast.MakeText(this, "you selected " + players[e.Which], ToastLength.Short).Show();  
  65.                          })  
  66.                         .Create();  
  67.                     alertDialog.Show();  
  68.                     break;  
  69.   
  70.                 case Resource.Id.btn_alertDialog_three:  
  71.                     var a = new AlertDialog.Builder(this);  
  72.                     string[] teams = new string[] {"骑士","公牛","快船","马刺","勇士" };  
  73.                     a.SetTitle("你认为下个赛季哪只球队能夺冠")  
  74.                         .SetSingleChoiceItems(teams, 0, (s, e) =>  
  75.                         {  
  76.                             Toast.MakeText(this, "you selected " + teams[e.Which], ToastLength.Short).Show();  
  77.                         })  
  78.                         .Create();  
  79.                     a.Show();  
  80.                     break;  
  81.   
  82.                  case Resource.Id.btn_alertDialog_four:  
  83.                     var b = new AlertDialog.Builder(this);  
  84.                     string[] menu = new string[] { "麻婆豆腐","羊蝎子","驴肉火烧","辣子鸡丁"};  
  85.                     checkItems = new bool[] {false,false,false,false};  
  86.                     b = b.SetIcon(Resource.Drawable.Icon)  
  87.                       .SetMultiChoiceItems(menu, checkItems, (s, e) => {  
  88.                             //Toast.MakeText(this, "you selected " + menu[e.Which], ToastLength.Short).Show();  
  89.                             checkItems[e.Which] = e.IsChecked;  
  90.                       })  
  91.                       .SetPositiveButton("确定", (s, e) => {  
  92.                           string result = string.Empty;  
  93.                           for (int i = 0; i < checkItems.Length; i++)  
  94.                           {  
  95.                               if (checkItems[i])  
  96.                               {  
  97.                                   result += menu[i] + ",";  
  98.                               }  
  99.                           }  
  100.                           Toast.MakeText(this, "you selected " + result, ToastLength.Short).Show();  
  101.                       });  
  102.                     b.Create();  
  103.                     b.Show();  
  104.                     break;  
  105.             }  
  106.         }  
  107.     }  

用法非常简单创建一个Builder对象后,调用Create方法创建一个AlertDialog对象,最后调用show方法显示出来,当然你也可以像这样直接new 一个AlertDialog对象

[html] view plain copy
print?
  1. var b = new AlertDialog.Builder(this);  
 设置一个setCancelable (true|false)看看有没有什么区别

3.通过Builder的setView()定制显示的AlertDialog

我们可以自定义一个与系统对话框不同的布局,然后调用setView()将我们的布局加载到AlertDialog上,上面我们来实现这个效果:


我就贴一下几个主要的布局和样式文件

1.对话框头部的取消按钮样式:在drawable文件下创建一个btn_selector_exit.xml文件,在这里要注意一点item下的属性android:background=“#dedede”,这样直接写会报错,我这里用的是换颜色,所以我在string.xml文件下写了两个颜色,大家要注意一下,我有点想不通的是为什么background属性直接写颜色代码会出错,有点郁闷,如果你有好的解释也可以告诉我这个android的 菜鸟

  <drawable name="pressed_color">#0cb026</drawable>
  <drawable name="default_color">#53cc66</drawable>

[html] view plain copy
print?
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3.   <item android:state_pressed="true" android:drawable="@drawable/exit_press"/>  
  4.   <item android:drawable="@drawable/exit"/>  
  5. </selector>  

2.底部两个按钮按下换背景色的样式新建一个btn_selector_choose.xml文件

[html] view plain copy
print?
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3.   <item android:state_pressed="true"  android:drawable="@drawable/pressed_color"  />  
  4.   <item  android:drawable="@drawable/default_color"  />  
  5. </selector>  

3.最重要的还是<?xml version="1.0" encoding="utf-8"?>
[html] view plain copy
print?
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:id="@+id/layout_relative"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent">  
  5. <!--头部-->  
  6.     <RelativeLayout  
  7.         android:id="@+id/layout_title"  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_alignParentLeft="true"  
  11.         android:layout_alignParentTop="true"  
  12.         android:background="#53cc66"  
  13.         android:padding="5dp">  
  14.         <TextView  
  15.             android:layout_width="match_parent"  
  16.             android:layout_height="wrap_content"  
  17.             android:layout_centerVertical="true"  
  18.             android:text="提示文本"  
  19.             android:textSize="18sp"  
  20.             android:textStyle="bold"  
  21.            android:textColor="#ffffff"  
  22.         />  
  23.         <Button  
  24.             android:id="@+id/btn_cancel"  
  25.             android:layout_width="30dp"  
  26.             android:layout_height="30dp"  
  27.             android:layout_alignParentRight="true"  
  28.             android:background="@drawable/btn_selector_exit" />  
  29.     </RelativeLayout>  
  30. <!--中间内容-->  
  31.     <LinearLayout  
  32.         android:id="@+id/layout_detail"  
  33.         android:layout_height="wrap_content"  
  34.         android:layout_width="match_parent"  
  35.         android:layout_below="@+id/layout_title"  
  36.         android:layout_centerInParent="true"  
  37.         android:orientation="vertical"  
  38.        android:background="#f1f1f1"  
  39.         android:padding="20dp"  
  40.         >  
  41.         <TextView  
  42.             android:layout_width="wrap_content"  
  43.             android:layout_height="wrap_content"  
  44.             android:text="通过setView方法定制alertDialog"  
  45.             android:textColor="#04AEDA"  
  46.             android:textSize="18sp" />  
  47.             <TextView  
  48.             android:layout_width="wrap_content"  
  49.             android:layout_height="wrap_content"  
  50.             android:text="作者:张林"  
  51.             android:textColor="#04AEDA"  
  52.             android:textSize="18sp" />  
  53.     </LinearLayout>  
  54. <!--底部按钮-->  
  55.     <LinearLayout  
  56.         android:layout_width="match_parent"  
  57.         android:layout_height="wrap_content"  
  58.         android:layout_below="@+id/layout_detail"  
  59.         android:orientation="horizontal"  
  60.         android:background="#f1f1f1"  
  61.         android:padding="5dp"  
  62.       >  
  63.         <Button  
  64.             android:id="@+id/btn_blog"  
  65.             android:layout_width="match_parent"  
  66.             android:layout_height="40dp"  
  67.             android:layout_weight="1"  
  68.             android:background="@drawable/btn_selector_choose"  
  69.             android:text="访问博客"  
  70.             android:textColor="#ffffff"  
  71.             android:textSize="20sp"  
  72.             android:layout_marginRight="5dp"  
  73.       />  
  74.         
  75.          <Button  
  76.             android:id="@+id/btn_close"  
  77.             android:layout_width="match_parent"  
  78.             android:layout_height="40dp"  
  79.             android:layout_weight="1"  
  80.             android:background="@drawable/btn_selector_choose"  
  81.             android:text="关闭对话框"  
  82.             android:textColor="#ffffff"  
  83.             android:textSize="20sp" />  
  84.       
  85.     </LinearLayout>  
  86. </RelativeLayout>  

mainactivity代码,这个布局我就不贴了。

[html] view plain copy
print?
  1.   public class MainActivity : Activity  
  2.     {  
  3.         private AlertDialog alertDialog = null;  
  4.         private AlertDialog.Builder builder = null;  
  5.         private Button btn_show = null;  
  6.         protected override void OnCreate(Bundle bundle)  
  7.         {  
  8.             base.OnCreate(bundle);  
  9.             SetContentView(Resource.Layout.Main);  
  10.              bindViewClick();  
  11.             builder = new AlertDialog.Builder(this);  
  12.            
  13.             LayoutInflater layoutInflater = LayoutInflater.From(this);  
  14.             var view_customer = layoutInflater.Inflate(Resource.Layout.view_dialog_custom, null, false);  
  15.   
  16.             builder.SetView(view_customer);  
  17.             builder.SetCancelable(false);  
  18.             alertDialog = builder.Create();  
  19.             view_customer.FindViewById(Resource.Id.btn_cancel).Click += (s, e) =>  
  20.             {  
  21.                 Toast.MakeText(this, "对话框已关闭", ToastLength.Short).Show();  
  22.                 alertDialog.Dismiss();  
  23.             };  
  24.             view_customer.FindViewById(Resource.Id.btn_blog).Click += delegate  
  25.             {  
  26.                 Toast.MakeText(this, "正在访问博客", ToastLength.Short).Show();  
  27.                 Uri uri = Uri.Parse("http://blog.csdn.net/kebi007");  
  28.                 Intent intent = new Intent(Intent.ActionView, uri);  
  29.                 StartActivity(intent);  
  30.                 alertDialog.Dismiss();  
  31.             };  
  32.             view_customer.FindViewById(Resource.Id.btn_close).Click += delegate  
  33.             {  
  34.                 Toast.MakeText(this, "对话框已关闭", ToastLength.Short).Show();  
  35.                 alertDialog.Dismiss();  
  36.             };  
  37.   
  38.             btn_show = FindViewById<Button>(Resource.Id.btn_show);  
  39.             btn_show.Click += delegate { alertDialog.Show(); };  
  40.         }  
  41.     }  

4.当然还有更高级的自定义的对话框,后面继续...........

原创粉丝点击