1、AlertDialog是一个信息提示框,当出现是,需要用户点击,才会消失
2、Toast也是一个信息提示框,出现后会更具设定的时间,自动消失
2、Toast也是一个信息提示框,出现后会更具设定的时间,自动消失
代码片段(2)[全屏查看所有代码]
1. [代码]main.xml
01 | <?xml version= "1.0" encoding= "utf-8" ?> |
02 |
03 | <LinearLayout xmlns:Android= "http://schemas.android.com/apk/res/android" |
04 | Android:orientation= "vertical" |
05 | Android:layout_width= "fill_parent" |
06 | Android:layout_height= "fill_parent" |
07 | > |
08 | <Button Android:id= "@+id/alert" |
09 | Android:layout_width= "fill_parent" |
10 | Android:layout_height= "wrap_content" |
11 | Android:text= "Make an alert" ></Button> |
12 | <Button Android:id= "@+id/toast" |
13 | Android:layout_width= "fill_parent" |
14 | Android:layout_height= "wrap_content" |
15 | Android:text= "Make a toast" ></Button> |
16 | </LinearLayout> |
2. [代码]MessageActivity.java
01 | package yyl.message; |
02 |
03 | import Android.app.Activity; |
04 | import Android.app.AlertDialog; |
05 | import Android.content.DialogInterface; |
06 | import Android.os.Bundle; |
07 | import Android.view.View; |
08 | import Android.widget.Button; |
09 | import Android.widget.Toast; |
10 |
11 | public class MessageActivity extends Activity { |
12 | //定义变量 |
13 | private Button alert = null ; |
14 | private Button toast = null ; |
15 | @Override |
16 | public void onCreate(Bundle savedInstanceState) { |
17 | super .onCreate(savedInstanceState); |
18 | setContentView(R.layout.main); |
19 | |
20 | //根据Id得到控件对象 |
21 | alert = (Button)findViewById(R.id.alert); |
22 | toast = (Button)findViewById(R.id.toast); |
23 | |
24 | //给按钮设定单击事件监听器 |
25 | alert.setOnClickListener( new View.OnClickListener() { |
26 | |
27 | @Override |
28 | public void onClick(View v) { |
29 | //显示AlertDialog |
30 | new AlertDialog.Builder(MessageActivity. this ).setTitle( "MessageDemo" ).setMessage( "Err" ).setNeutralButton( "close" , new DialogInterface.OnClickListener() { |
31 | |
32 | //点击AlertDialog上的按钮的事件处理代码 |
33 | @Override |
34 | public void onClick(DialogInterface dialog, int which) { |
35 | System.out.println( "yangyulin" ); |
36 | |
37 | } |
38 | }).show(); |
39 | |
40 | } |
41 | }); |
42 | toast.setOnClickListener( new View.OnClickListener() { |
43 | |
44 | //显示Toast |
45 | @Override |
46 | public void onClick(View v) { |
47 | |
48 | Toast.makeText(MessageActivity. this , "<Clink,Clink>" , Toast.LENGTH_SHORT).show(); |
49 | |
50 | } |
51 | }); |
52 | } |
53 | } |