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();
代码养活自己