【转】Android消息提示框和对话框

上一篇 / 下一篇  2012-12-19 10:48:57 / 个人分类:android

转帖地址:http://www.cnblogs.com/chunhui588/archive/2010/10/01/Android_Toast_And_AlertDialog.html

http://blubels.blog.163.com/blog/static/184257897201151553015831/

http://www.apkbus.com/android-39868-1-1.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
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"android:layout_height="fill_parent"
    android:orientation="vertical">
    <LinearLayoutandroid:layout_width="fill_parent"
        android:layout_height="wrap_content"android:orientation="horizontal">
        <TextViewandroid:layout_width="wrap_content"
            android:layout_height="wrap_content"android:text="Name:"/>
        <EditTextandroid:layout_width="fill_parent"
            android:layout_height="wrap_content"android:id="@+id/editName"></EditText>
    </LinearLayout>
    <LinearLayoutandroid:layout_width="fill_parent"
        android:layout_height="wrap_content"android:orientation="horizontal">
        <TextViewandroid:layout_width="wrap_content"
            android:layout_height="wrap_content"android:text="Age:"></TextView>
        <EditTextandroid: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);
 
newAlertDialog.Builder(this).setTitle("Add Employee").setView(
       viewAddEmployee).setPositiveButton("OK",
       newDialogInterface.OnClickListener() {
           @Override
           publicvoidonClick(DialogInterface dialog,intwhich) {
               insertEmployee();
           }
       }).setNegativeButton("Cancel",
       newDialogInterface.OnClickListener() {
           @Override
           publicvoidonClick(DialogInterface dialog,intwhich) {
                
           }
       }).show();
1
  

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

 

image

 


TAG:

引用 删除 葡萄冰   /   2014-03-04 09:44:06
 

评分:0

我来说两句

Open Toolbar