【Android开发】自定义AlertDialog

来源:互联网 发布:重庆旅游 知乎 编辑:程序博客网 时间:2024/06/11 10:04

学习了自己编写一个自定义的AlertDialog两天时间,下面就介绍一下我用了的这种方法和遇到一些问题。

步骤一:

编写你想自定义的AlertDialog的xml布局文件。例如customalertdialog.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout      android:id="@+id/relativelayout1"    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="wrap_content"    android:layout_height="wrap_content"   >     <Button      android:id="@+id/button1"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:paddingLeft="10dp" />       <TextView          android:id="@+id/showdatetitle"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:layout_below="@id/smallphoto"          android:text="日期"          android:textSize="25dp" />      <EditText              android:id="@+id/showdate"             android:layout_width="fill_parent"             android:layout_height="wrap_content"             android:layout_toRightOf="@id/showdatetitle"             android:layout_below="@id/takephoto"                   />               <TextView android:id="@+id/name"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:layout_below="@id/showdatetitle"             android:text="名字"              android:textSize="25dp" />         <EditText              android:id="@+id/writeshopname"             android:layout_width="fill_parent"             android:layout_height="wrap_content"             android:layout_toRightOf="@id/name"             android:layout_below="@id/showdate"             android:layout_alignParentRight="true"        />          </RelativeLayout>
我们这个布局就是简单的编写Dialog里面 有一个 “日期”后面跟EditText,“名字”后面跟一个EditText

二、编写Activity里面的代码

因为Dialog一般是按某个按钮之后触发的,就像我做的那个一样,所以就把构造的代码放在对应的事件监听器里面

LayoutInflater factory=LayoutInflater.from(ChiguonaActivity.this);final View mView=factory.inflate(R.layout.customdialog, null);new AlertDialog.Builder(this).setTitle("添加").setView(mView).   setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stub//}}).setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stub}}).create().show();

其中
LayoutInflater factory=LayoutInflater.from(mActivity.this);final View mView=factory.inflate(R.layout.customalertdialog, null);

的作用是从xml里面取出这个布局,变成View对象,这样才能做对应的操作

new AlertDialog.Builder(this).setTitle("添加").setView(mView).   setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stub//自由操作}}).setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stub//自由操作}}).create().show();
这里就是构造这个自定义的AlertDialog,在Eclipse编程的时候在AlertDialog.Build(this)后面按了一点之后,你可以查看到智能提示的各种函数,这里用到了setTitle即是设定标题的函数,还有关键的setView()函数,正是把我们上面获取到的布局mView放进去,Dialog就能按我们自己的布局来构造。

然后接下来有setPositiveButton和setNegativeButton两个方法,它们就是设定AlertDialog下方的按钮,就是一般我们看到的对话框的“确定”“取消”,那么它们有什么区别?其实它们也就是本身封装好的几个Button,名字不一样而已,具体执行什么操作由我们自己在里面写。最后,可以发现,添加.creat()和.show()把对话框展示出来;

三,关于自定义AlertDialog里面的触发事件

看到我的xml里面一开始是不是加了一个button,我这样做的目的就是想说明一下怎么调用这些button的按钮事件(区别于setPositiveButton这些button,它属于自定义的)

我一开始是这样写的

Button bt=(Button)findViewById(R.id.button1);

bt.setOnclickListener(new onClickListener(.........));

因为我们知道只要在xml定义了的空间,R.id都可以找到,就可以获得它的控制。但是这样一直报错。查看了错误信息,是空指针错误。

不科学啊~

后来我尝试着这样改动

Button bt=(Button)mView.findViewById(R.id.button1);

正常运行了。

这个mView应该还记得,就是我们把自定义的xml转为的VIEW对象。需要这样才可以获取到这个AlertDialog里面的Button的各种控制。

这样我就推测:一般我们在onCreate()里面写的    控件类 xx=(控件类)findViewById(R.id.xxx);这里的findViewById前面是隐含了一个当前activity的xml加载后的对象,就是默认这种是当前activity的xml的findViewById,是不能获取到非当前activity的控件的。


上面就是我大概总结出来的经验,可能有些纰漏的地方,希望共同进步。




0 0