MonoAndroid: 使用 AlertDialog



在这篇文章中,我将演示如何使用警告对话框(AlertDialog),它是用来显示发送到用户的自定义消息,这是高度可扩展的。通常你最多可以设置三个自定义文本按钮,你可以像操作普通按钮一样在对他们进行操作,在Android上一切都是异步的。

本文共分三个部分

  • AlertBox原型  -使用AlertDialog的普通方式

  • ListView AlertBox -包含 listview AlertDialog

  • 自定义布局的AlertBox -包含自定义布局的AlertDialog

    AlertBox被广泛应用与android系统中,它无处不在,.当我玩手机游戏时,经常会碰到类似是否要退出的询问.

    Step By Step we move forward

1.      通过选择文件->新建->项目->模板->Android->Android Application并命名为AndroidAlertDialogDemo




  1. 打开文件夹Resources->layout ,编辑Main.Axml文件,在上面添加三个按钮,按如下代码修改Main.Axml


<strong><?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:layout_height="fill_parent">
    <Button
        android:id="@+id/myButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Base AlertBox" />
    <Button
        android:id="@+id/myButton1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="AlertBox with ListView" />
    <Button
        android:id="@+id/myButton2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="AlertBox with CustomView" />
</LinearLayout>
</strong>

1.      3.打开MainActivity.cs,找到button1,对其添中单击事件.

Button btnNormalDialog = FindViewById<Button> (Resource.Id.myButton);			
btnNormalDialog.Click += methodInvokeBaseAlertDialog;

1.      4.在按钮单击事件中添加AlertDialog


void methodInvokeBaseAlertDialog (object sender, EventArgs e)
{
 var dlgAlert = (new AlertDialog.Builder (this)).Create ();
 dlgAlert.SetMessage ("Hello from alertDialog");
 dlgAlert.SetTitle ("Alert Dialog");
 dlgAlert.SetButton ("OK", handllerNotingButton);
 dlgAlert.SetButton2 ("Cancel", handllerNotingButton);
 dlgAlert.SetButton3 ("Nothing", handllerNotingButton);
 dlgAlert.Show ();
}


void handllerNotingButton (object sender, DialogClickEventArgs e)
{
	AlertDialog objAlertDialog = sender as AlertDialog;
	Button btnClicked = objAlertDialog.GetButton (e.Which);
	Toast.MakeText (this, "you clicked on " + btnClicked.Text, ToastLength.Long).Show ();
}
o 用方法 (AlertDialog.Builder(this)) 创建 AlertDialog 对象.
o Using builder object create AlertDialog object.
o 用SetMessage 和SetTitle 创建主体信息和标题.
o 分别用SetButton, SetButton2 and SetButton3三个方法创建三个按钮,并对这三个按钮编辑事件.
o 最后用方法 Show() 显示 AlertDialog.
5. 编译并运行 DEMO I






6.在第二部分中我们将要在AlertDialog中显示ListView.需要创建BaseAdapter并插入ListView


internal class AlertListViewAdapter: BaseAdapter<string>
{
	Activity _context = null;
	List<String> _lstDataItem = null;

	public AlertListViewAdapter (Activity context, List<String> lstDataItem)
	{
		_context = context;
		_lstDataItem = lstDataItem;			
	}

	#region implemented abstract members of BaseAdapter

	public override long GetItemId (int position)
	{
		return position;
	}

	public override View GetView (int position, View convertView, ViewGroup parent)
	{
		if (convertView == null)
			convertView = _context.LayoutInflater.Inflate (Android.Resource.Layout.SimpleListItem1, null);

		(convertView.FindViewById<TextView> (Android.Resource.Id.Text1))
			.SetText (this [position], TextView.BufferType.Normal);

		return convertView;
	}

	public override int Count {
		get {
			return _lstDataItem.Count;
		}
	}

	#endregion

	#region implemented abstract members of BaseAdapter

	public override string this [int index] {
		get {
			return _lstDataItem [index];
		}
	}

	#endregion

}

7.在MainActivity.cs中为第二个按钮添加单击事件

btnNormalDialog = FindViewById<Button> (Resource.Id.myButton1);			
btnNormalDialog.Click += methodInvokeAlertDialogWithListView;

创建字符串类型 的集合并在listview显示

_lstDataItem.Add ("Person 1");
_lstDataItem.Add ("Person 2");
_lstDataItem.Add ("Person 3");
_lstDataItem.Add ("Person 4");

8.在AlertDialog添加listview,首先创建ListView对象,绑定adapter,用SetView方法把ListView对象添加到AlertDialog

#region ListView AlertDialog

	void methodInvokeAlertDialogWithListView (object sender, EventArgs e)
	{
		var dlgAlert = (new AlertDialog.Builder (this)).Create ();
		dlgAlert.SetTitle ("List View Alert Dialog");
		var listView = new ListView (this);
		listView.Adapter = new AlertListViewAdapter (this, _lstDataItem);
		listView.ItemClick += listViewItemClick;
		dlgAlert.SetView (listView);
		dlgAlert.SetButton ("OK", handllerNotingButton);
		dlgAlert.Show ();
	}

	void listViewItemClick (object sender, AdapterView.ItemClickEventArgs e)
	{
		Toast.MakeText (this, "you clicked on " + _lstDataItem [e.Position], ToastLength.Short).Show ();
	}

#endregion




    9.编译并运行DEMO II



    10第二幅图是点击“Person 3”记录的结果

    11在Demo III, 创建自定义布局的AlertDialog.layout文件夹下新建 AlertDialogLayout.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:layout_height="fill_parent">
        <RelativeLayout
            android:minWidth="25px"
            android:minHeight="25px"
            android:layout_width="fill_parent"
            android:layout_height="110.7dp"
            android:id="@+id/relativeLayout1">
            <ImageView
                android:src="@android:drawable/ic_menu_gallery"
                android:id="@+id/imageView1"
                android:layout_width="75.3dp"
                android:layout_height="wrap_content" />
            <TextView
                android:text="Medium Text"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:layout_width="146.0dp"
                android:layout_height="90.7dp"
                android:id="@+id/textView1"
                android:layout_toRightOf="@+id/imageView1"
                android:layout_marginRight="10.7dp" />
            <ImageView
                android:src="@android:drawable/ic_menu_gallery"
                android:id="@+id/imageView2"
                android:layout_width="75.3dp"
                android:layout_toRightOf="@+id/textView1"
                android:layout_height="wrap_content" />
        </RelativeLayout>
    </LinearLayout>
        
    

      12.在上面的文件中使用了以下两个图像文件



      13.在单击事件中添加以下代码


      void methodInvokeAlertDialogWithCustomView (object sender, EventArgs e)
      {
      	var dlgAlert = (new AlertDialog.Builder (this)).Create ();
      	dlgAlert.SetTitle ("Sample Alert Dialog");
      	var viewAD = this.LayoutInflater.Inflate (Resource.Layout.AlertDialogLayout, null);
      
      	viewAD.FindViewById<TextView> (Resource.Id.textView1).SetText ("Message From Layout",TextView.BufferType.Normal);
      	viewAD.FindViewById<ImageView> (Resource.Id.imageView1).SetImageResource(Resource.Drawable.Icon1);
      	viewAD.FindViewById<ImageView> (Resource.Id.imageView2).SetImageResource(Resource.Drawable.Icon2);
      
      	dlgAlert.SetView (viewAD);
      	dlgAlert.SetButton ("OK", handllerNotingButton);
      	dlgAlert.Show ();
      }
      

      14.编译并运行 DEMO III













          评论
          添加红包

          请填写红包祝福语或标题

          红包个数最小为10个

          红包金额最低5元

          当前余额3.43前往充值 >
          需支付:10.00
          成就一亿技术人!
          领取后你会自动成为博主和红包主的粉丝 规则
          hope_wisdom
          发出的红包
          实付
          使用余额支付
          点击重新获取
          扫码支付
          钱包余额 0

          抵扣说明:

          1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
          2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

          余额充值