系出名门Android(3) - 对话框(Dialog)和通知(Notification)

[索引页]
[源码下载]


系出名门Android(3) - 对话框(Dialog)和通知(Notification)


作者:webabcd


介绍
在 Android 中种对话框及各种通知效果的应用
  • 常用对话框的使用,弹出式对话框、日期选择对话框、时间选择对话框、进度条对话框  
  • 通知(出现在通知列表)和提示性通知(Toast)的演示


1、常用对话框的演示
res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:orientation="vertical" android:layout_width="fill_parent" 
        android:layout_height="fill_parent"> 
         
        <TextView android:id="@+id/txtMsg" android:layout_width="wrap_content" 
                android:layout_height="wrap_content"></TextView> 
                 
        <Button android:id="@+id/btn1" android:layout_width="wrap_content" 
                android:layout_height="wrap_content"></Button> 
        <Button android:id="@+id/btn2" android:layout_width="wrap_content" 
                android:layout_height="wrap_content"></Button> 
        <Button android:id="@+id/btn3" android:layout_width="wrap_content" 
                android:layout_height="wrap_content"></Button> 
        <Button android:id="@+id/btn4" android:layout_width="wrap_content" 
                android:layout_height="wrap_content"></Button> 
        <Button android:id="@+id/btn5" android:layout_width="wrap_content" 
                android:layout_height="wrap_content"></Button> 
        <Button android:id="@+id/btn6" android:layout_width="wrap_content" 
                android:layout_height="wrap_content"></Button> 
                 
</LinearLayout>
 
res/layout/view.xml
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:orientation="vertical" android:layout_width="fill_parent" 
        android:layout_height="fill_parent"> 
         
        <TextView android:text="我是一个 View" 
                android:layout_width="wrap_content" android:layout_height="wrap_content"> 
        </TextView> 
         
</LinearLayout>
 
res/values/array.xml
<?xml version="1.0" encoding="utf-8"?> 
<resources> 
        <!-- 
                定义一个名为 ary 的 string 类型的数组 
        
--> 
        <string-array name="ary"> 
                <item>项目 1</item> 
                <item>项目 2</item> 
        </string-array> 
</resources>
 
Main.java
package com.webabcd.dialog; 

import java.util.Calendar; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.DatePickerDialog; 
import android.app.Dialog; 
import android.app.ProgressDialog; 
import android.app.TimePickerDialog; 
import android.app.DatePickerDialog.OnDateSetListener; 
import android.app.TimePickerDialog.OnTimeSetListener; 
import android.content.DialogInterface; 
import android.content.DialogInterface.OnClickListener; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.DatePicker; 
import android.widget.TextView; 
import android.widget.TimePicker; 
import android.widget.Button; 

public class Main extends Activity { 
        /** Called when the activity is first created. */ 
        @Override 
        public void onCreate(Bundle savedInstanceState) { 
                super.onCreate(savedInstanceState); 
                setContentView(R.layout.main); 

                // 弹出式对话框的 Demo。先调用 Builder(),在 Create(), 需要显示对话框的是后再调用 show() 
                AlertDialog dialog = new AlertDialog.Builder(this).setTitle("弹出对话框").create(); 
                dialog.show(); 
                 
                // 以下是各种对话框的 Demo 
                MyButtonClickListener listener = new MyButtonClickListener(); 
                Button btn1 = (Button) this.findViewById(R.id.btn1); 
                btn1.setText("简单的对话框的 Demo"); 
                btn1.setOnClickListener(listener); 

                Button btn2 = (Button) this.findViewById(R.id.btn2); 
                btn2.setText("包括常用设置的对话框(数据来自 xml)"); 
                btn2.setOnClickListener(listener); 

                Button btn3 = (Button) this.findViewById(R.id.btn3); 
                btn3.setText("弹出的对话框的内容是一个 View"); 
                btn3.setOnClickListener(listener); 

                Button btn4 = (Button) this.findViewById(R.id.btn4); 
                btn4.setText("日期选择对话框"); 
                btn4.setOnClickListener(listener); 

                Button btn5 = (Button) this.findViewById(R.id.btn5); 
                btn5.setText("时间选择对话框"); 
                btn5.setOnClickListener(listener); 

                Button btn6 = (Button) this.findViewById(R.id.btn6); 
                btn6.setText("进度条对话框"); 
                btn6.setOnClickListener(listener); 
        } 

        class MyButtonClickListener implements View.OnClickListener { 
                @Override 
                public void onClick(View v) { 
                         
                        // 具体的对话框的实现可以通过重写 onCreateDialog 完成 
                         
                        switch (v.getId()) { 
                        case R.id.btn1: 
                                Main.this.showDialog(0); 
                                break
                        case R.id.btn2: 
                                Main.this.showDialog(1); 
                                break
                        case R.id.btn3: 
                                Main.this.showDialog(2); 
                                break
                        case R.id.btn4: 
                                Main.this.showDialog(3); 
                                break
                        case R.id.btn5: 
                                Main.this.showDialog(4); 
                                break
                        case R.id.btn6: 
                                Main.this.showDialog(5); 
                                break
                        } 
                } 
        } 

        @Override 
        public Dialog onCreateDialog(int id) { 
                switch (id) { 

                case 0: 
                        // 一个简单的弹出对话框 
                        return new AlertDialog.Builder(this).setTitle("这是一个简单的弹出对话框的 Demo"
                                        .create(); 

                case 1: 
                        // 一个相对复杂的弹出对话框 
                        return new AlertDialog.Builder(this
                                        .setTitle("标题"// 设置标题 
                                        // .setCustomTitle(View) // 以一个 View 作为标题    
                                        .setIcon(R.drawable.icon01) // 设置标题图片 
                                        // .setMessage("信息") // 需要显示的弹出内容 
                                        .setPositiveButton("确定"new OnClickListener() { // 设置弹框的确认按钮所显示的文本,以及单击按钮后的响应行为 
                                                @Override 
                                                public void onClick(DialogInterface a0, int a1) { 
                                                        TextView txtMsg = (TextView) Main.this.findViewById(R.id.txtMsg); 
                                                        txtMsg.append("单击了对话框上的“确认”按钮\n"); 
                                                } 
                                        }) 
                                        .setItems(R.array.ary, new DialogInterface.OnClickListener() { // 弹框所显示的内容来自一个数组。数组中的数据会一行一行地依次排列 
                                                public void onClick(DialogInterface dialog,        int which) { 
                                                } 
                                        }) 
                                        // 其他常用方法如下 
                                        // .setMultiChoiceItems(arg0, arg1, arg2) 
                                        // .setSingleChoiceItems(arg0, arg1, arg2) 
                                        // .setNeutralButton(arg0, arg1) 
                                        // .setNegativeButton(arg0, arg1) 
                                        .create(); 

                case 2: 
                        // 弹出对话框为指定的 View 的 Demo 
                        return new AlertDialog.Builder(this).setTitle("此对话框的内容是一个 View"
                                        .setView(this.findViewById(R.layout.view)).create(); 

                case 3: 
                        // 弹出日期选择对话框 
                        Calendar c = Calendar.getInstance(); 
                        return new DatePickerDialog(thisnew OnDateSetListener() { 
                                @Override 
                                public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { 
                                        TextView txtMsg = (TextView) Main.this.findViewById(R.id.txtMsg); 
                                        txtMsg.append("新设置的日期为:" + String.valueOf(year) + "-" 
                                                        + String.valueOf(monthOfYear) + "-" 
                                                        + String.valueOf(dayOfMonth) + "\n"); 
                                } 
                        }, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE)); 

                case 4: 
                        // 弹出时间选择对话框 
                        Calendar c2 = Calendar.getInstance(); 
                        return new TimePickerDialog(thisnew OnTimeSetListener() { 
                                @Override 
                                public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 
                                        TextView txtMsg = (TextView) Main.this.findViewById(R.id.txtMsg); 
                                        txtMsg.append("新设置的时间为:"    
                                                        + String.valueOf(hourOfDay) + ":" 
                                                        + String.valueOf(minute) + "\n"); 
                                } 

                        }, c2.get(Calendar.HOUR), c2.get(Calendar.MINUTE), true); 

                case 5: 
                        // 弹出进度条对话框 
                        ProgressDialog progress = new ProgressDialog(this); 
                        progress.setMessage("loading..."); 
                        return progress; 

                default
                        return null
                } 
        } 
}
 
2、各种提示效果的演示
res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:orientation="vertical" android:layout_width="fill_parent" 
        android:layout_height="fill_parent"> 
         
        <Button android:id="@+id/btn1" android:layout_width="wrap_content" 
                android:layout_height="wrap_content"></Button> 
        <Button android:id="@+id/btn2" android:layout_width="wrap_content" 
                android:layout_height="wrap_content"></Button> 
        <Button android:id="@+id/btn3" android:layout_width="wrap_content" 
                android:layout_height="wrap_content"></Button> 
        <Button android:id="@+id/btn4" android:layout_width="wrap_content" 
                android:layout_height="wrap_content"></Button> 
                 
</LinearLayout>
 
res/layout/view.xml
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:orientation="vertical" android:layout_width="fill_parent" 
        android:layout_height="fill_parent"> 
         
        <TextView android:id="@+id/txtMsg" android:layout_width="wrap_content" 
                android:layout_height="wrap_content"> 
        </TextView> 
                 
</LinearLayout>
 
Main.java
package com.webabcd.notification; 

import android.app.Activity; 
import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 
import android.widget.Toast; 

public class Main extends Activity { 

        /** Called when the activity is first created. */ 
        @Override 
        public void onCreate(Bundle savedInstanceState) { 
                super.onCreate(savedInstanceState); 
                setContentView(R.layout.main); 

                // 通过 Tost.makeText().show() 来实现提示性的通知效果 
                // 短时间的提示性通知的 Demo 
                Button btn1 = (Button) this.findViewById(R.id.btn1); 
                btn1.setText("短时间提示"); 
                btn1.setOnClickListener(new Button.OnClickListener() { 
                        public void onClick(View v) { 
                                Toast.makeText(Main.this"我是短时间提示", Toast.LENGTH_SHORT).show(); 
                        } 
                }); 

                // 长时间的提示性通知的 Demo 
                Button btn2 = (Button) this.findViewById(R.id.btn2); 
                btn2.setText("长时间提示"); 
                btn2.setOnClickListener(new Button.OnClickListener() { 
                        public void onClick(View v) { 
                                Toast.makeText(Main.this"我是长时间提示", Toast.LENGTH_LONG).show(); 
                        } 
                }); 

                // 以一个 View 作为提示性通知的 Demo 
                Button btn3 = (Button) this.findViewById(R.id.btn3); 
                btn3.setText("以一个 View 做提示"); 
                btn3.setOnClickListener(new Button.OnClickListener() { 
                        public void onClick(View v) { 
                                View view = inflateView(R.layout.view); 
                                TextView txtMsg = (TextView) view.findViewById(R.id.txtMsg); 
                                txtMsg.setText("提示内容"); 

                                Toast toast = new Toast(Main.this); 
                                toast.setView(view); 
                                toast.setDuration(Toast.LENGTH_LONG); 
                                toast.show(); 
                        } 
                }); 

                 
                Button btn4 = (Button) this.findViewById(R.id.btn4); 
                btn4.setText("发出一个通知(Notification)"); 
                btn4.setOnClickListener(new Button.OnClickListener() { 
                        public void onClick(View v) { 
                                // 实例化通知管理器 
                                NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 

                                // 指定单击通知后所打开的详细的通知页面(单击通知后打开 NotificationView) 
                                PendingIntent contentIntent = PendingIntent.getActivity( 
                                                Main.this, 0, new Intent(Main.this,        NotificationView.class), 0); 

                                // 实例化一个通知,并指定其图标和标题(在提示栏上显示) 
                                Notification n = new Notification(R.drawable.icon01, "我是滚动的通知信息我是滚动的通知信息我是滚动的通知信息", System.currentTimeMillis()); 
                                 
                                // 设置通知的发送人和通知的详细内容(打开提示栏后在通知列表中显示) 
                                n.setLatestEventInfo(Main.this"通知发送人""我是详细的通知信息我是详细的通知信息我是详细的通知信息", contentIntent); 

                                // 100 毫秒延迟后,震动 250 毫秒,暂停 100 毫秒后,再震动 500 毫秒 
                                n.vibrate = new long[] { 100, 250, 100, 500 }; 
                                 
                                // 发出通知(其中第一个参数为通知标识符) 
                                nm.notify(0, n); 
                        } 
                }); 
        } 

        // 将指定的 xml 资源转换为一个 View 
        private View inflateView(int resource) { 
                LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
                return vi.inflate(resource, null); 
        } 

        // 打开详细通知页后此 Activity 会被 Pause,从详细通知页返回后此 Activity 会被 Resume 
        @Override 
        protected void onPause() { 
                // TODO Auto-generated method stub 
                super.onPause(); 
                 
                Log.d("MyDebug""onPause"); 
        } 
         
        @Override 
        protected void onResume() { 
                // TODO Auto-generated method stub 
                super.onResume(); 
                 
                Log.d("MyDebug""onResume"); 
        } 
}
 
NotificationView.java
package com.webabcd.notification; 

import android.app.Activity; 
import android.app.NotificationManager; 
import android.os.Bundle; 
import android.widget.TextView; 

// 单击通知列表的某个通知后,所打开的详细的通知页 
public class NotificationView extends Activity { 
        protected void onCreate(Bundle savedInstanceState) { 
                super.onCreate(savedInstanceState); 
                setContentView(R.layout.view); 

                TextView txtMsg = (TextView)this.findViewById(R.id.txtMsg); 
                txtMsg.setText("点通知之后所链接到的 Activity"); 
                 
                NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
                // 取消显示在通知列表中的指定通知(参数为通知标识符) 
                nm.cancel(0); 
                 
                // 需要关闭此 Activity 的话就 finish 它既可 
                // this.finish(); 
        } 
}
 
 



     本文转自webabcd 51CTO博客,原文链接:http://blog.51cto.com/webabcd/342014,如需转载请自行联系原作者
上一篇:修改统计信息自动收集时间窗口


下一篇:Go 标准库 —— time 常用类型和方法