xamarin android alertdialog详解(几种弹出窗口)转载(https://blog.csdn.net/kebi007/article/details/52599522)

版权声明:本文为博主原创文章,未经博主允许转载随意。 https://blog.csdn.net/kebi007/article/details/52599522

说明一下:学习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


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


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

 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>


     
     
  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文件


     
     
  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"?>


     
     
  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. <Button
  75. android:id= "@+id/btn_close"
  76. android:layout_width= "match_parent"
  77. android:layout_height= "40dp"
  78. android:layout_weight= "1"
  79. android:background= "@drawable/btn_selector_choose"
  80. android:text= "关闭对话框"
  81. android:textColor= "#ffffff"
  82. android:textSize= "20sp" />
  83. </LinearLayout>
  84. </RelativeLayout>


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


     
     
  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.             builder.SetView(view_customer);
  16.             builder.SetCancelable( false);
  17.             alertDialog = builder.Create();
  18.             view_customer.FindViewById(Resource.Id.btn_cancel).Click += (s, e) =>
  19.             {
  20.                 Toast.MakeText( this, "对话框已关闭", ToastLength. Short).Show();
  21.                 alertDialog.Dismiss();
  22.             };
  23.             view_customer.FindViewById(Resource.Id.btn_blog).Click += delegate
  24.             {
  25.                 Toast.MakeText( this, "正在访问博客", ToastLength. Short).Show();
  26.                 Uri uri = Uri.Parse( "http://blog.csdn.net/kebi007");
  27.                 Intent intent = new Intent(Intent.ActionView, uri);
  28.                 StartActivity(intent);
  29.                 alertDialog.Dismiss();
  30.             };
  31.             view_customer.FindViewById(Resource.Id.btn_close).Click += delegate
  32.             {
  33.                 Toast.MakeText( this, "对话框已关闭", ToastLength. Short).Show();
  34.                 alertDialog.Dismiss();
  35.             };
  36.             btn_show = FindViewById<Button>(Resource.Id.btn_show);
  37.             btn_show.Click += delegate { alertDialog.Show(); };
  38.         }
  39.     }

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

有兴趣的可以关注一下我的微信公众号,分享一些编程相关的经典文章

猜你喜欢

转载自www.cnblogs.com/dingling275445130/p/11320630.html