AlertDialog.Builder弹出自定义Layout窗口

来源:互联网 发布:知乎日报与读读日报 编辑:程序博客网 时间:2024/06/06 10:53

------------------------------------------------------------------------------------------------------

       此文章仅作为学习交流所用

       转载或引用请务必注明原文地址:

       http://blog.csdn.net/luzhenrong45/article/details/21340435

       或联系作者:luzhenrong45@gmail.com 

       谢谢!                       

------------------------------------------------------------------------------------------------------


一.使用AlertDialog创建弹出窗口步骤

前面说过,使用AlertDialog.Builder弹出窗口,一般,是下面几个步骤.

(一)创建AltrtDialog.Builder对象,该对象是AlterDialog的创建器

Public ConstructorsAlertDialog.Builder(Context context)
Constructor using a context for this builder and the AlertDialog it creates.
AlertDialog.Builder(Context context, int theme)
Constructor using a context and theme for this builder and the AlertDialog it creates.

(二)调用 AltrtDialog.Builder的方法为对话框设定图标,标题,内容等.

AlertDialog.BuildersetIcon(Drawable icon)
Set the Drawable to be used in the title.
AlertDialog.BuildersetIcon(int iconId)
Set the resource id of the Drawable to be used in the title.
AlertDialog.BuildersetMessage(CharSequence message)
Set the message to display.
AlertDialog.BuildersetMessage(int messageId)
Set the message to display using the given resource id.
AlertDialog.BuildersetOnKeyListener(DialogInterface.OnKeyListener onKeyListener)
Sets the callback that will be called if a key is dispatched to the dialog.
AlertDialog.BuildersetPositiveButton(int textId, DialogInterface.OnClickListener listener)
Set a listener to be invoked when the positive button of the dialog is pressed.
AlertDialog.BuildersetPositiveButton(CharSequence text, DialogInterface.OnClickListener listener)
Set a listener to be invoked when the positive button of the dialog is pressed.
AlertDialog.BuildersetTitle(CharSequence title)
Set the title displayed in the Dialog.
AlertDialog.BuildersetTitle(int titleId)
Set the title using the given resource id.
AlertDialog.BuildersetView(View view)
Set a custom view to be the contents of the Dialog.

(三)调用 AltrtDialog.Builder的create()方法创建对话框

AlertDialogcreate()
Creates a AlertDialog with the arguments supplied to this builder.

(四)调用 AltrtDialog.Builder的show()方法显示对话框

AlertDialogshow()
Creates a AlertDialog with the arguments supplied to this builder and show()'s the dialog

二.使用AlertDialog.Builder加载自定义View

   按照上面的步骤,使用的是默认的AlertDialog.Builder的窗口显示方式,如果想要显示内容丰富的弹出窗口,如里面有一些输入框之类的,如下面的图片所示,那么,就需要我们使用AlertDialog.Builder.setView(View v)方法加载自定义的View来作为窗口的显示方式了.

.

(一) 这里自定义的布局文件为 order.xml.

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:background="#ffffffff"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <LinearLayout  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:background="#ff000000"  
  12.         android:layout_marginTop="41dip"  
  13.         android:layout_marginBottom="41dip"  
  14.         android:layout_marginLeft="41dip"  
  15.         android:layout_marginRight="41dip"  
  16.         android:orientation="vertical" >  
  17.         <TextView  
  18.             android:layout_width="fill_parent"  
  19.             android:layout_height="wrap_content"  
  20.             android:text="请输入欲预约图书的书号: "  
  21.             android:textSize="25dip" >  
  22.         </TextView>  
  23.     </LinearLayout>  
  24.   
  25.     <LinearLayout  
  26.         android:layout_width="fill_parent"  
  27.         android:layout_height="wrap_content"  
  28.         android:layout_marginLeft="41dip"  
  29.         android:layout_marginRight="41dip"  
  30.         android:layout_marginBottom="24dip"  
  31.         android:orientation="horizontal" >  
  32.   
  33.         <TextView  
  34.             android:layout_width="wrap_content"  
  35.             android:layout_height="wrap_content"  
  36.             android:text="预约书号: "  
  37.             android:textColor="#000000"  
  38.             android:textSize="25dip" >  
  39.         </TextView>  
  40.   
  41.         <EditText  
  42.             android:id="@+id/orderEditSH"  
  43.             android:layout_width="fill_parent"  
  44.             android:layout_height="wrap_content"  
  45.             android:text="10009"  
  46.             android:textColor="#000000"  
  47.             android:textSize="25dip" >  
  48.         </EditText>  
  49.     </LinearLayout>  
  50.   
  51.     <LinearLayout  
  52.         android:layout_width="fill_parent"  
  53.         android:layout_height="wrap_content"  
  54.         android:layout_gravity="center_horizontal"  
  55.         android:layout_marginLeft="41dip"  
  56.         android:layout_marginRight="41dip"  
  57.         android:layout_marginBottom="41dip"  
  58.         android:orientation="horizontal" >  
  59.   
  60.         <Button  
  61.             android:id="@+id/startOrder_btn"  
  62.             android:layout_width="wrap_content"  
  63.             android:layout_height="wrap_content"  
  64.             android:text="开始预约图书   "  
  65.             android:textSize="25dip" >  
  66.         </Button>  
  67.   
  68.         <Button  
  69.             android:id="@+id/managerOrder_btn"  
  70.             android:layout_width="wrap_content"  
  71.             android:layout_height="wrap_content"  
  72.             android:text="管理个人预约  "  
  73.             android:textSize="25dip" >  
  74.         </Button>  
  75.     </LinearLayout>  
  76.   
  77. </LinearLayout>  

(二)下面,我们通过代码,加载这个自定义View

    首先,获取需要加载的布局文件order.xml, 这里采用的是LayoutInflater,而不是我们平时使用的 findViewById( ).LayoutInflater的作用类似于 findViewById(),不同点是LayoutInflater是用来找layout文件夹下的xml布局文件,并且实例化!而 findViewById()是找具体某一个xml下的具体 widget控件(如:Button,TextView等)。使用LayoutInflater来获取布局文件有三种方式:

第一种方式:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. LayoutInflater inflater = LayoutInflater.from(this);    
  2. View layout = inflater.inflate(R.layout.order, null);   
第二种方式:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. LayoutInflater inflater = getLayoutInflater();    
  2. View layout = inflater.inflate(R.layout.order, null);    
第三种方式:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);    
  2. View layout = inflater.inflate(R.layout.main, null);  


   以前我们在Activity里面使用某个控件时,通常是直接使用findViewById()方法来获取这个控件,而如果我们要使用AlertDialog.Builder显示的自定义Layout里面的控件,需要注意要显示地用View对象调用findViewById()这个方法.如上面创建的是View layout对象.那么,需要使用如下访求获取EditText  orderEditSH.

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. EditText orderBookNum=(EditText)layout.findViewById(R.id.orderEditSH);  

使用AlertDialog.Builder弹出上面自定义窗口的完整代码如下:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. LayoutInflater inflater = LayoutInflater.from(this);  
  2. View layout<span style="font-family: Arial;">=inflater.inflate(R.layout.order,null);</span>  
  3.           
  4. AlertDialog.Builder builder =new AlertDialog.Builder(Order.this);  
  5. builder.setView(view);  
  6. builder.setCancelable(false);  
  7. builder.create().show();  
  8. Button startOrder_btn=(Button) layout.findViewById(R.id.startOrder_btn);  
  9. Button managerOrder_btn=(Button) layout.findViewById(R.id.managerOrder_btn);  
  10. EditText orderBookNum=(EditText)layout.findViewById(R.id.orderEditSH);  
  11.           
  12. startOrder_btn.setOnClickListener(new OnClickListener(){  
  13.     public void onClick(View v) {  
  14.         //加上你要实现的代码       
  15.         }  
  16.     });  
  17. managerOrder_btn.setOnClickListener(new OnClickListener(){  
  18.     public void onClick(View v) {//加上你要实现的代码  
  19.         }  
  20.     });  

0 0
原创粉丝点击