AlertDialog.Builder弹出自定义Layout窗口
2014-03-16 22:37
399 查看
------------------------------------------------------------------------------------------------------
此文章仅作为学习交流所用
转载或引用请务必注明原文地址:
/article/8467057.html
或联系作者:luzhenrong45@gmail.com
谢谢!
------------------------------------------------------------------------------------------------------
一.使用AlertDialog创建弹出窗口步骤
前面说过,使用AlertDialog.Builder弹出窗口,一般,是下面几个步骤.
(一)创建AltrtDialog.Builder对象,该对象是AlterDialog的创建器
(二)调用 AltrtDialog.Builder的方法为对话框设定图标,标题,内容等.
(三)调用 AltrtDialog.Builder的create()方法创建对话框
(四)调用 AltrtDialog.Builder的show()方法显示对话框
二.使用AlertDialog.Builder加载自定义View
按照上面的步骤,使用的是默认的AlertDialog.Builder的窗口显示方式,如果想要显示内容丰富的弹出窗口,如里面有一些输入框之类的,如下面的图片所示,那么,就需要我们使用AlertDialog.Builder.setView(View
v)方法加载自定义的View来作为窗口的显示方式了.
.
![](http://img.blog.csdn.net/20140316220542593?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbHV6aGVucm9uZzQ1/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
(一) 这里自定义的布局文件为 order.xml.
(二)下面,我们通过代码,加载这个自定义View
首先,获取需要加载的布局文件order.xml, 这里采用的是LayoutInflater,而不是我们平时使用的 findViewById( ).LayoutInflater的作用类似于 findViewById(),不同点是LayoutInflater是用来找layout文件夹下的xml布局文件,并且实例化!而
findViewById()是找具体某一个xml下的具体 widget控件(如:Button,TextView等)。使用LayoutInflater来获取布局文件有三种方式:
第一种方式:
以前我们在Activity里面使用某个控件时,通常是直接使用findViewById()方法来获取这个控件,而如果我们要使用AlertDialog.Builder显示的自定义Layout里面的控件,需要注意要显示地用View对象调用findViewById()这个方法.如上面创建的是View layout对象.那么,需要使用如下访求获取EditText
orderEditSH.
使用AlertDialog.Builder弹出上面自定义窗口的完整代码如下:
此文章仅作为学习交流所用
转载或引用请务必注明原文地址:
/article/8467057.html
或联系作者:luzhenrong45@gmail.com
谢谢!
------------------------------------------------------------------------------------------------------
一.使用AlertDialog创建弹出窗口步骤
前面说过,使用AlertDialog.Builder弹出窗口,一般,是下面几个步骤.
(一)创建AltrtDialog.Builder对象,该对象是AlterDialog的创建器
Public Constructors | |
---|---|
AlertDialog.Builder(Context context) Constructor using a context for this builder and the AlertDialogit creates. | |
AlertDialog.Builder(Context context, int theme) Constructor using a context and theme for this builder and the AlertDialogit creates. |
AlertDialog.Builder | setIcon(Drawable icon) Set the Drawableto be used in the title. |
AlertDialog.Builder | setIcon(int iconId) Set the resource id of the Drawableto be used in the title. |
AlertDialog.Builder | setMessage(CharSequence message) Set the message to display. |
AlertDialog.Builder | setMessage(int messageId) Set the message to display using the given resource id. |
AlertDialog.Builder | setOnKeyListener(DialogInterface.OnKeyListener onKeyListener) Sets the callback that will be called if a key is dispatched to the dialog. |
AlertDialog.Builder | setPositiveButton(int textId, DialogInterface.OnClickListener listener) Set a listener to be invoked when the positive button of the dialog is pressed. |
AlertDialog.Builder | setPositiveButton(CharSequence text, DialogInterface.OnClickListener listener) Set a listener to be invoked when the positive button of the dialog is pressed. |
AlertDialog.Builder | setTitle(CharSequence title) Set the title displayed in the Dialog. |
AlertDialog.Builder | setTitle(int titleId) Set the title using the given resource id. |
AlertDialog.Builder | setView(View view) Set a custom view to be the contents of the Dialog. |
AlertDialog | create() Creates a AlertDialogwith the arguments supplied to this builder. |
AlertDialog | show() Creates a AlertDialogwith the arguments supplied to this builder and show()'s the dialog |
按照上面的步骤,使用的是默认的AlertDialog.Builder的窗口显示方式,如果想要显示内容丰富的弹出窗口,如里面有一些输入框之类的,如下面的图片所示,那么,就需要我们使用AlertDialog.Builder.setView(View
v)方法加载自定义的View来作为窗口的显示方式了.
.
(一) 这里自定义的布局文件为 order.xml.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffffff" android:orientation="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#ff000000" android:layout_marginTop="41dip" android:layout_marginBottom="41dip" android:layout_marginLeft="41dip" android:layout_marginRight="41dip" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="请输入欲预约图书的书号: " android:textSize="25dip" > </TextView> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="41dip" android:layout_marginRight="41dip" android:layout_marginBottom="24dip" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="预约书号: " android:textColor="#000000" android:textSize="25dip" > </TextView> <EditText android:id="@+id/orderEditSH" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="10009" android:textColor="#000000" android:textSize="25dip" > </EditText> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginLeft="41dip" android:layout_marginRight="41dip" android:layout_marginBottom="41dip" android:orientation="horizontal" > <Button android:id="@+id/startOrder_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="开始预约图书 " android:textSize="25dip" > </Button> <Button android:id="@+id/managerOrder_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="管理个人预约 " android:textSize="25dip" > </Button> </LinearLayout> </LinearLayout>
(二)下面,我们通过代码,加载这个自定义View
首先,获取需要加载的布局文件order.xml, 这里采用的是LayoutInflater,而不是我们平时使用的 findViewById( ).LayoutInflater的作用类似于 findViewById(),不同点是LayoutInflater是用来找layout文件夹下的xml布局文件,并且实例化!而
findViewById()是找具体某一个xml下的具体 widget控件(如:Button,TextView等)。使用LayoutInflater来获取布局文件有三种方式:
第一种方式:
LayoutInflater inflater = LayoutInflater.from(this); View layout = inflater.inflate(R.layout.order, null);第二种方式:
LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.order, null);第三种方式:
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); View layout = inflater.inflate(R.layout.main, null);
以前我们在Activity里面使用某个控件时,通常是直接使用findViewById()方法来获取这个控件,而如果我们要使用AlertDialog.Builder显示的自定义Layout里面的控件,需要注意要显示地用View对象调用findViewById()这个方法.如上面创建的是View layout对象.那么,需要使用如下访求获取EditText
orderEditSH.
EditText orderBookNum=(EditText)layout.findViewById(R.id.orderEditSH);
使用AlertDialog.Builder弹出上面自定义窗口的完整代码如下:
LayoutInflater inflater = LayoutInflater.from(this); View layout=inflater.inflate(R.layout.order,null); AlertDialog.Builder builder =new AlertDialog.Builder(Order.this); builder.setView(view); builder.setCancelable(false); builder.create().show(); Button startOrder_btn=(Button) layout.findViewById(R.id.startOrder_btn); Button managerOrder_btn=(Button) layout.findViewById(R.id.managerOrder_btn); EditText orderBookNum=(EditText)layout.findViewById(R.id.orderEditSH); startOrder_btn.setOnClickListener(new OnClickListener(){ public void onClick(View v) { //加上你要实现的代码 } }); managerOrder_btn.setOnClickListener(new OnClickListener(){ public void onClick(View v) {//加上你要实现的代码 } });
相关文章推荐
- AlertDialog.Builder弹出自定义Layout窗口
- AlertDialog.Builder弹出自定义Layout窗口
- AlertDialog的自定义View以及弹出后的亮度控制
- Android之自定义对话框AlertDialog.Builder+getLayoutInflater().inflate
- 关于带有EditText的自定义AlertDialog,不能弹出软件盘的解决方法
- 关于AlertDialog.Builder的自定义
- android 自定义Dialog.Builder弹出框
- ProgressDialog,SharedPreferences,Toast自定义,versionName ,new AlertDialog.Builder和下载更新
- 解决在自定义AlertDialog中EditText无法弹出软键盘的问题
- Android listView,自定义Adapter,另The constructor AlertDialog.Builder(MyAdapter) is undefined问题
- android AlertBuilder创建的dialog,使用自定义样式
- Android弹出框PopupWindow,Dialog,AlertDialog.Builder,AlertDialog的一知半解
- 自定义AlertDialog.Builder
- 弹出对话框AlertDialog.Builder
- 使用AlertDialog.Builder自定义对话框
- android 自定义弹出框AlertDialog
- alertdialog.builder 自定义弹窗
- 弹出对话框AlertDialog.Builder使用笔记
- AlertDialog.Builder弹出对话框
- 自定义AlertDialog.Builder对话框