首页 > 学院 > 开发设计 > 正文

AlertDialog和AlertDialog.Builder两者区别

2019-11-06 07:10:26
字体:
来源:转载
供稿:网友

 那我们先了解什么是AlertDialog?什么是AlertDialog.Builder?且两者有什么区别?    AlertDialog是Dialog的一个直接子类,AlertDialog也是Android系统当中最常用的对话框之一。    一个AlertDialog可以有两个以上的Button,可以对一个AlertDialog设置相应的信息。比如title,massage,setSingleChoiceItems,setPositiveButton,setNegativeButton等等。。。。   但不能直接通过AlertDialog的构造函数来生产一个AlertDialog。研究AlertDialog的源码发现AlertDialog所有的构造方法都是写保护的所以不能通过:AlertDialog alertDialog  = new AlertDialog();来得到。AlertDialog构造方法源码如下:

[java] view plaincopyPRotected AlertDialog(Context context) {         this(context, com.android.internal.R.style.Theme_Dialog_Alert);     }         protected AlertDialog(Context context, int theme) {         super(context, theme);         mAlert = new AlertController(context, this, getWindow());     }         protected AlertDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {         super(context, com.android.internal.R.style.Theme_Dialog_Alert);         setCancelable(cancelable);         setOnCancelListener(cancelListener);         mAlert = new AlertController(context, this, getWindow());     }    

只能通过:

[java] view plaincopyAlertDialog.Builder alertDialog  =new  AlertDialog.Builder(this);    

来得到。

那就通过一个具体的实例来说说吧(这里用一个最常用的对话框为例):

[java] view plaincopypackage com.oyah;         import android.app.Activity;     import android.app.AlertDialog;     import android.content.DialogInterface;     import android.os.Bundle;         public class TestsActivity extends Activity {         public void onCreate(Bundle savedInstanceState) {             super.onCreate(savedInstanceState);             setContentView(R.layout.main);                 AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);             alertDialog                     .setTitle(”title”)                     .setMessage(”message”)                     .setPositiveButton(”okBuuon”,                             new DialogInterface.OnClickListener() {                                     @Override                                public void onClick(DialogInterface dialog,                                         int which) {                                     }                             })                     .setNegativeButton(”exitButton”,                             new DialogInterface.OnClickListener() {                                     @Override                                public void onClick(DialogInterface dialog,                                         int which) {                                     }                             }).[b]setCancelable(false).[/b]create().show();         }     }    

针对AlertDialog中设置了确定和取消按钮,一般来说确定为执行某个动作,取消就是不执行,但是如果用户点击了系统的Back键,此时就会将AlertDialog关闭,而并没有执行预期的取消的操作。此时需要关注一个方法setCancelable(false) 该方法定义设置该AlertDialog是否可以被Back键取消,如果不设置默认为true 下面是一些扩展: 根据AlertDialog.Builder 创建 相应的 AlertDialog

[java] view plaincopyAlertDialog alertDialogs = alertDialog.create();    

用dismiss();方法来清除创建过的AlertDialog

[java] view plaincopyalertDialogs.dismiss();    

以上所采用的都是AlertDialog 系统默认的布局 现在说自定义布局的情况 并添加一个用于取消AlertDialog 的 Button 定义其布局 main.xml

[java] view plaincopy<?xml version=“1.0” encoding=“utf-8”?>  <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”      android:orientation=”horizontal”      android:layout_width=”fill_parent”      android:layout_height=”fill_parent”      android:padding=”10dp”      >  <LinearLayout       android:orientation=”vertical”      android:layout_width=”wrap_content”      android:layout_height=”wrap_content”      >   </LinearLayout>  </LinearLayout>  

通过LayoutInflater 得到上面 main.xml 布局文件

[java] view plaincopyview = this.getLayoutInflater().inflate(R.layout.main, null);  

指定AlertDialog.Builder 所需的布局 并返回目标AlertDialog

[java] view plaincopyalertDialog.setView(view);    alertDialogs= alertDialog.create();  

通过 view.findViewById() 得到 目标View 然后设置其内容 如:

[java] view plaincopyTextView title = (TextView) view.findViewById(R.id.title);  title.setTextSize(20);  title.setTextColor(Color.RED);  title.setText(”Title”);            TextView message = (TextView) view.findViewById(R.id.message);  message.setText(”message”);  

显示对话框 AlertDialog

[java] view plaincopyfindViewById(R.id.button).setOnClickListener(new OnClickListener(){              public void onClick(View v) {                  // TODO Auto-generated method stub                  alertDialogs.show();              }          });  

清除对话框 AlertDialog

[java] view plaincopyview.setOnClickListener(new OnClickListener(){              public void onClick(View v) {                  // TODO Auto-generated method stub                  alertDialogs.dismiss();              }          });  

 

</td> <script> var html = document.getElementById("artContent").innerHTML; document.getElementById("artContent").innerHTML = html; </script> </tr> </tbody> </table> <div id="viewerPlaceHolder" style="width: 717px; height: 700px; display: none; margin: 0 auto;"> </div>
上一篇:遇到的Tomcat问题

下一篇:编码格式

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表