Android提示框与通知的使用

1.通知

  Android 3.0以前使用的方法

1             NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
2             Notification notification = new Notification(R.drawable.dss,
3                     "通知到了", System.currentTimeMillis());
4             notification.flags = Notification.FLAG_AUTO_CANCEL;
5             Intent intent = new Intent();
6             PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
7                     intent, 0);
8             notification.setLatestEventInfo(this, "标题", "内容", contentIntent);
9             nm.notify(0, notification);

  替代方法

 1             Intent intent = new Intent();
 2             PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
 3                     intent, 0);
 4             Notification noti = new Notification.Builder(this)
 5                     .setTicker("通知到了")
 6                     .setContentTitle("标题")
 7                     .setContentText("内容")
 8                     .setAutoCancel(true)
 9                     .setContentIntent(contentIntent)
10                     .setSmallIcon(R.drawable.dss)
11                     .setLargeIcon(
12                             BitmapFactory.decodeResource(getResources(),
13                                     R.drawable.dss)).build();
14             NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
15             nm.notify(0, noti);

 

2.对话框

 1             AlertDialog.Builder builder = new AlertDialog.Builder(this);
 2             builder.setTitle("对话框标题");
 3             builder.setMessage("对话框内容");
 4             builder.setPositiveButton("确定", new OnClickListener() {
 5                 @Override
 6                 public void onClick(DialogInterface dialog, int which) {
 7                     //Toast.makeText(getApplicationContext(), "确定被点击了", Toast.LENGTH_SHORT).show();
 8                 }
 9             });
10             builder.setNegativeButton("取消", new OnClickListener() {
11                 @Override
12                 public void onClick(DialogInterface dialog, int which) {
13                     //Toast.makeText(getApplicationContext(), "确定被点击了", Toast.LENGTH_SHORT).show();
14                 }
15             });
16             //禁止响应返回键
17             builder.setCancelable(false);
18             builder.create().show();

 

3.单选对话框

 1             AlertDialog.Builder builder = new AlertDialog.Builder(this);
 2             builder.setTitle("请选择:");
 3             final String[] items = {"喜欢", "不喜欢"};
 4             builder.setSingleChoiceItems(items, 0, new OnClickListener() {
 5                 @Override
 6                 public void onClick(DialogInterface dialog, int which) {
 7                     //单击后退出单选对话框
 8                     dialog.dismiss();
 9                 }
10             });
11             builder.create().show();


4.多选对话框

 1             AlertDialog.Builder builder = new AlertDialog.Builder(this);
 2             builder.setTitle("请选择你最爱吃的水果");
 3             final String[] items={"苹果","梨","菠萝","香蕉","黄瓜"};
 4             final boolean[] result =new boolean[]{true,false,true,false,false};
 5             builder.setMultiChoiceItems(items, result, new OnMultiChoiceClickListener() {
 6                 @Override
 7                 public void onClick(DialogInterface dialog, int which, boolean isChecked) {
 8                     //Toast.makeText(getApplicationContext(), items[which]+isChecked, 0).show();
 9                     result[which] = isChecked;
10                 }
11             });
12             builder.setPositiveButton("提交", new OnClickListener() {
13                 @Override
14                 public void onClick(DialogInterface dialog, int which) {
15                     StringBuffer sb = new StringBuffer();
16                     for(int i=0;i<result.length;i++){
17                         //if(result[i]){
18                         //    sb.append(" " + items[i]);
19                         //}
20                     }
21                     //Toast.makeText(getApplicationContext(), "您选中了"+sb.toString(), 0).show();
22                 }
23             });
24             builder.show();//作用同 builder.create().show();

 

5.带进度条的对话框

 1             final ProgressDialog pd = new ProgressDialog(this);
 2             pd.setTitle("警告");
 3             pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
 4             pd.setMax(100);
 5             pd.setMessage("正在处理数据,请稍等。。。");
 6             pd.show();
 7             new Thread(){
 8                 public void run() {
 9                     for(int i = 0;i<100;i++){
10                         try {
11                             Thread.sleep(40);
12                         } catch (InterruptedException e) {
13                             e.printStackTrace();
14                         }
15                         pd.setProgress(i);
16                     }
17                     pd.dismiss();
18                 };
19             }.start();

 

转载于:https://www.cnblogs.com/diysoul/p/3971055.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值