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

public class MainActivity : Activity
{
int count = 1;
private Button btn_alertDialog_one, btn_alertDialog_two, btn_alertDialog_three, btn_alertDialog_four; private bool[] checkItems;
private AlertDialog alertDialog = null;
private AlertDialog.Builder builder = null;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
bindViewClick();
} private void bindViewClick()
{
btn_alertDialog_four = FindViewById<Button>(Resource.Id.btn_alertDialog_four);
btn_alertDialog_three = FindViewById<Button>(Resource.Id.btn_alertDialog_three);
btn_alertDialog_two = FindViewById<Button>(Resource.Id.btn_alertDialog_two);
btn_alertDialog_one = FindViewById<Button>(Resource.Id.btn_alertDialog_one); btn_alertDialog_one.Click += delegate { onClick(btn_alertDialog_one); };
btn_alertDialog_two.Click += delegate { onClick(btn_alertDialog_two); };
btn_alertDialog_three.Click += delegate { onClick(btn_alertDialog_three); };
btn_alertDialog_four.Click += delegate { onClick(btn_alertDialog_four); };
}
private void onClick(View v)
{
switch (v.Id)
{
case Resource.Id.btn_alertDialog_one:
alertDialog = null;
builder = new AlertDialog.Builder(this);
alertDialog = builder
.SetTitle("sure")
.SetMessage("are you sure exit?")
.SetNegativeButton("cancel", (s, e) =>
{
Toast.MakeText(this, "you click cancel", ToastLength.Short).Show();
})
.SetPositiveButton("sure", (s, e) =>
{
Toast.MakeText(this, "you click sure", ToastLength.Short).Show();
})
.SetNeutralButton("neutra", (s, e) => {
Toast.MakeText(this, "you click neutra", ToastLength.Short).Show();
})
.Create(); //创建alertDialog对象 alertDialog.Show();
var dialog = new AlertDialog.Builder(this);
break; case Resource.Id.btn_alertDialog_two:
alertDialog = null;
builder = new AlertDialog.Builder(this);
string[] players = new string[] {"杜兰特","汤普森","考辛斯","卡戴珊"};
alertDialog = builder
.SetIcon(Resource.Drawable.players)
.SetTitle("选择你喜欢的球员")
.SetItems(players, (s, e) =>
{
Toast.MakeText(this, "you selected " + players[e.Which], ToastLength.Short).Show();
})
.Create();
alertDialog.Show();
break; case Resource.Id.btn_alertDialog_three:
var a = new AlertDialog.Builder(this);
string[] teams = new string[] {"骑士","公牛","快船","马刺","勇士" };
a.SetTitle("你认为下个赛季哪只球队能夺冠")
.SetSingleChoiceItems(teams, 0, (s, e) =>
{
Toast.MakeText(this, "you selected " + teams[e.Which], ToastLength.Short).Show();
})
.Create();
a.Show();
break; case Resource.Id.btn_alertDialog_four:
var b = new AlertDialog.Builder(this);
string[] menu = new string[] { "麻婆豆腐","羊蝎子","驴肉火烧","辣子鸡丁"};
checkItems = new bool[] {false,false,false,false};
b = b.SetIcon(Resource.Drawable.Icon)
.SetMultiChoiceItems(menu, checkItems, (s, e) => {
//Toast.MakeText(this, "you selected " + menu[e.Which], ToastLength.Short).Show();
checkItems[e.Which] = e.IsChecked;
})
.SetPositiveButton("确定", (s, e) => {
string result = string.Empty;
for (int i = 0; i < checkItems.Length; i++)
{
if (checkItems[i])
{
result += menu[i] + ",";
}
}
Toast.MakeText(this, "you selected " + result, ToastLength.Short).Show();
});
b.Create();
b.Show();
break;
}
}
}

用法非常简单创建一个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>

<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/exit_press"/>
<item android:drawable="@drawable/exit"/>
</selector>

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

<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/pressed_color" />
<item android:drawable="@drawable/default_color" />
</selector>

3.最重要的还是<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_relative"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--头部-->
<RelativeLayout
android:id="@+id/layout_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#53cc66"
android:padding="5dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="提示文本"
android:textSize="18sp"
android:textStyle="bold"
android:textColor="#ffffff"
/>
<Button
android:id="@+id/btn_cancel"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:background="@drawable/btn_selector_exit" />
</RelativeLayout>
<!--中间内容-->
<LinearLayout
android:id="@+id/layout_detail"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_below="@+id/layout_title"
android:layout_centerInParent="true"
android:orientation="vertical"
android:background="#f1f1f1"
android:padding="20dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="通过setView方法定制alertDialog"
android:textColor="#04AEDA"
android:textSize="18sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="作者:张林"
android:textColor="#04AEDA"
android:textSize="18sp" />
</LinearLayout>
<!--底部按钮-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/layout_detail"
android:orientation="horizontal"
android:background="#f1f1f1"
android:padding="5dp"
>
<Button
android:id="@+id/btn_blog"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_weight="1"
android:background="@drawable/btn_selector_choose"
android:text="访问博客"
android:textColor="#ffffff"
android:textSize="20sp"
android:layout_marginRight="5dp"
/> <Button
android:id="@+id/btn_close"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_weight="1"
android:background="@drawable/btn_selector_choose"
android:text="关闭对话框"
android:textColor="#ffffff"
android:textSize="20sp" /> </LinearLayout>
</RelativeLayout>

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

  public class MainActivity : Activity
    {
        private AlertDialog alertDialog = null;
        private AlertDialog.Builder builder = null;
        private Button btn_show = null;
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.Main);
             bindViewClick();
            builder = new AlertDialog.Builder(this);
         
            LayoutInflater layoutInflater = LayoutInflater.From(this);
            var view_customer = layoutInflater.Inflate(Resource.Layout.view_dialog_custom, null, false);             builder.SetView(view_customer);
            builder.SetCancelable(false);
            alertDialog = builder.Create();
            view_customer.FindViewById(Resource.Id.btn_cancel).Click += (s, e) =>
            {
                Toast.MakeText(this, "对话框已关闭", ToastLength.Short).Show();
                alertDialog.Dismiss();
            };
            view_customer.FindViewById(Resource.Id.btn_blog).Click += delegate
            {
                Toast.MakeText(this, "正在访问博客", ToastLength.Short).Show();
                Uri uri = Uri.Parse("http://blog.csdn.net/kebi007");
                Intent intent = new Intent(Intent.ActionView, uri);
                StartActivity(intent);
                alertDialog.Dismiss();
            };
            view_customer.FindViewById(Resource.Id.btn_close).Click += delegate
            {
                Toast.MakeText(this, "对话框已关闭", ToastLength.Short).Show();
                alertDialog.Dismiss();
            };             btn_show = FindViewById<Button>(Resource.Id.btn_show);
            btn_show.Click += delegate { alertDialog.Show(); };
        }
    }

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

xamarin android alertdialog详解的更多相关文章

  1. Xamarin.Android通知详解

    一.发送通知的机制 在日常的app应用中经常需要使用通知,因为服务.广播后台活动如果有事件需要通知用户,则需要通过通知栏显示,而在Xamarin.Android下的通知需要获取Notification ...

  2. Xamarin Android Gestures详解

    通过Gesture的监听我们将实现一个,手指的快速滑动显示坐标的变化,我们先来看一看效果图: 1.Android中手势交互的执行顺序 1.手指触碰屏幕时,触发MotionEvent事件! 2.该事件被 ...

  3. Android进阶(十四)Android Adapter详解

    Android Adapter详解 Android是完全遵循MVC模式设计的框架,Activity是Controller,layout是View.因为layout五花八门,很多数据都不能直接绑定上去, ...

  4. Xamarin+Prism开发详解七:Plugin开发与打包测试

    有了上章[Xamarin+Prism开发详解六:DependencyService与IPlatformInitializer的关系]的基础,现在来理解Plugin开发就简单了. 本文实例代码地址:ht ...

  5. Android Notification 详解(一)——基本操作

    Android Notification 详解(一)--基本操作 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/Notification 文中如有纰 ...

  6. Android Notification 详解——基本操作

    Android Notification 详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 前几天项目中有用到 Android 通知相关的内容,索性把 Android Notificatio ...

  7. Android ActionBar详解

    Android ActionBar详解 分类: Android2014-04-30 15:23 1094人阅读 评论(0) 收藏 举报 androidActionBar   目录(?)[+]   第4 ...

  8. Android 签名详解

    Android 签名详解 AndroidOPhoneAnt设计模式Eclipse  在Android 系统中,所有安装 到 系统的应用程序都必有一个数字证书,此数字证书用于标识应用程序的作者和在应用程 ...

  9. Android编译系统详解(一)

    ++++++++++++++++++++++++++++++++++++++++++ 本文系本站原创,欢迎转载! 转载请注明出处: http://blog.csdn.net/mr_raptor/art ...

随机推荐

  1. 轻松学习 JavaScript——第 6 部分:JavaScript 箭头函数

    JavaScript箭头函数是ECMAScript 6中引入的编写函数表达式的一种简便方法.通常,在JavaScript中,可以通过两种方式创建函数: 函数语句. 函数表达式. 可以如下所示创建函数语 ...

  2. Spark SQL中的几种join

    1.小表对大表(broadcast join) 将小表的数据分发到每个节点上,供大表使用.executor存储小表的全部数据,一定程度上牺牲了空间,换取shuffle操作大量的耗时,这在SparkSQ ...

  3. openvpn技术实现客户端直接访问远程机器中docker内容器的实现与原理

    传统开发中如果要从开发机中访问服务器中的docker中的服务可能可能需要如下方案: 利用docker run的-p属性直接映射端口到服务器中 优点:客户端直接访问服务器就可以访问到docker容器. ...

  4. Django contrib Comments 评论模块详解

    老版本的Django中自带一个评论框架.但是从1.6版本后,该框架独立出去了,也就是本文的评论插件. 这个插件可给models附加评论,因此常被用于为博客文章.图片.书籍章节或其它任何东西添加评论. ...

  5. System.Security.Cryptography.RSA.FromXmlString 系统找不到指定的文件和X509读取证书文件系统找不到指定的文件异常

    前言: 最近公司增加服务器,在新增加的服务器中发现一些问题. 1.应用程序在读取证书文件中出现"系统找不到指定的文件."异常,但是已经确认证书文件存在.本地测试也可以读取,就在新增 ...

  6. NOIP2014无线网络发射器选址改编1

    问题描述 随着智能手机的日益普及,人们对无线网的需求日益增大.某城市决定对城市内的公共场所覆盖无线网. 假设该城市的布局为由严格平行的129条东西向街道和129条南北向街道所形成的网格状,并且相邻的平 ...

  7. webpack入门之打包html,css,js,img(一)

    webpack到底是什么,网上一大堆介绍的东西,越看越不知道说的什么,所以今天打算自己来记录一下这段时间学习webpack的成果, webpack就是打包文件用的,html,css,js,img,为什 ...

  8. js二级事件模型的处理细节

    一.纠正网络上的一个误传--“IE不支持事件捕获” 可以在浏览器中运行上面demo,在各主流浏览器中,鼠标移上都可以分别触发捕获与冒泡事件的监听函数,所以IE也是支持事件捕获的,连IE6都支持,只是在 ...

  9. 使用.Net Core+EF7 CodeFirst(2)

    上一篇的话,说了下怎么使用EF7 实现 CodeFirst去生成数据库, 其实还有好多问题的,这次一点一点的解决吧,都挺简单,不过零零散散的,, 1.读取配置文件,获得链接字符串 2.使用数据库进行增 ...

  10. hdu 5288||2015多校联合第一场1001题

    pid=5288">http://acm.hdu.edu.cn/showproblem.php?pid=5288 Problem Description OO has got a ar ...