Android消息提示框和对话框

转自:http://www.cnblogs.com/chunhui588/archive/2010/10/01/Android_Toast_And_AlertDialog.html

在某些情况下需要向用户弹出提示消息,如显示错误信息,收到短消息等,Android提供两种弹出消息的方式,消息提示框toasts和对话框alerts。

 

Toast是一种短暂的消息提示,显示一段时间后不需要用户交互会自动消失,所以用来显示一些建议性的不太重要的消息,如提示用户后台一个任务完成了。

使用Toast来弹出提示消息也很简单,调用Toast类的静态方法makeText():

 

public static Toast makeText (Context context, CharSequence text, int duration)

context: 调用的上下文,通常为Application或Activity对象

text: 显示的消息

duration: 显示的时间长短,为 Toast.LENGTH_LONG或Toast.LENGTH_SHORT

 

如可以这样调用:Toast.makeText(this, "Deleted Successfully!", Toast.LENGTH_LONG).show(); 效果如下:

 

image

 

AlertDialog类似于传统的模式对话框,需要与用户交互后才会关闭。

最简单的创建AlertDialog对话框的方法是使用AlertDialog的嵌套类Builder,它有下面几个主要的方法:

 

setMessage(): 设置显示的消息内容

setTitle() 和setIcon(): 设置对话框的标题栏的文字和图标

setPositiveButton(), setNeutralButton()和setNegativeButton(): 设置对话框的按钮,包括按钮显示的文字,按钮点击的事件

setView(): 设置对话框显示一个自定义的视图

 

自定义视图addemployee.xml代码如下, 需要注意的是布局文件的名称只能包含“a-z0-9_.”,不然就会报这样的错误:“Invalid file name: must contain only [a-z0-9_.]”

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<? 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:orientation = "vertical" >
     < LinearLayout  android:layout_width = "fill_parent"
         android:layout_height = "wrap_content"  android:orientation = "horizontal" >
         < TextView  android:layout_width = "wrap_content"
             android:layout_height = "wrap_content"  android:text = "Name:"  />
         < EditText  android:layout_width = "fill_parent"
             android:layout_height = "wrap_content"  android:id = "@+id/editName" ></ EditText >
     </ LinearLayout >
     < LinearLayout  android:layout_width = "fill_parent"
         android:layout_height = "wrap_content"  android:orientation = "horizontal" >
         < TextView  android:layout_width = "wrap_content"
             android:layout_height = "wrap_content"  android:text = "Age:" ></ TextView >
         < EditText  android:layout_width = "fill_parent"
             android:layout_height = "wrap_content"  android:id = "@+id/editAge"  android:inputType = "number" ></ EditText >
     </ LinearLayout >
</ LinearLayout >

 

生成对话框的代码如下所示:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
     LayoutInflater layoutInflater = LayoutInflater.from( this );
viewAddEmployee = layoutInflater.inflate(R.layout.addemployee, null );
 
new  AlertDialog.Builder( this ).setTitle( "Add Employee" ).setView(
        viewAddEmployee).setPositiveButton( "OK" ,
        new  DialogInterface.OnClickListener() {
            @Override
            public  void  onClick(DialogInterface dialog, int  which) {
                insertEmployee();
            }
        }).setNegativeButton( "Cancel" ,
        new  DialogInterface.OnClickListener() {
            @Override
            public  void  onClick(DialogInterface dialog, int  which) {
                
            }
        }).show();
?
1
  

这里先加载了一个自定义的视图, 并通过setView()设置对话框显示这个自定义视图, 并添加了两个按钮和相应的点击事件, 运行效果如下:

 

image

 

希望本文对您有所帮助。

猜你喜欢

转载自cshbbrain.iteye.com/blog/1844625