一.Toast
// 弹出吐司 Toast.makeText(getApplicationContext(), "吐司", 0).show();
二.对话框:
1.确定取消对话框
public void click1(View view){ //对话框的创建器 AlertDialog.Builder builder = new Builder(this); builder.setTitle("我是对话框"); builder.setMessage("对话框显示的内容"); builder.setPositiveButton("确定", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(), "确定被点击了", 0).show(); } }); builder.setNegativeButton("取消", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //什么都不写默认实现就是关闭掉对话框 } }); builder.setCancelable(false); builder.create().show(); }
2.单选对话框
public void click2(View view){ //对话框的创建器 AlertDialog.Builder builder = new Builder(this); builder.setTitle("请选择您的性别"); final String[] items = {"男","女","未知"}; builder.setSingleChoiceItems(items, 2, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(), "您的性别:"+items[which], 0).show(); dialog.dismiss(); } }); builder.create().show(); }
3.多选对话框
public void click3(View view){ //对话框的创建器 AlertDialog.Builder builder = new Builder(this); builder.setTitle("请选择你最爱吃的水果"); final String[] items={"苹果","梨","菠萝","香蕉","黄瓜"}; final boolean[] result =new boolean[]{true,false,true,false,false}; builder.setMultiChoiceItems(items, result, new OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { Toast.makeText(getApplicationContext(), items[which]+isChecked, 0).show(); result[which] = isChecked; } }); builder.setPositiveButton("提交", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { StringBuffer sb = new StringBuffer(); for(int i=0;i<result.length;i++){ if(result[i]){ sb.append(items[i]+","); } } Toast.makeText(getApplicationContext(), "您选中了,"+sb.toString(), 0).show(); } }); builder.show(); }
4.进度条对话框
public void click4(View view){ ProgressDialog pd = new ProgressDialog(this); pd.setTitle("提醒"); pd.setMessage("正在加载数据...请稍等。"); pd.show(); }
5.带进度的进度条对话框
public void click5(View view){ final ProgressDialog pd = new ProgressDialog(this); pd.setTitle("提醒"); pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); pd.setMax(100); pd.setMessage("正在加载数据...请稍等。"); pd.show(); new Thread(){ public void run() { for(int i = 0;i<100;i++){ try { Thread.sleep(40); } catch (InterruptedException e) { e.printStackTrace(); } pd.setProgress(i); } pd.dismiss(); }; }.start(); }
三.notification----Android手机通知栏
//3.0以前的版本需要用过时的方法才行,否则报错 public void click(View view){ NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Notification notification = new Notification(R.drawable.notification, "我是一个通知", System.currentTimeMillis()); notification.flags = Notification.FLAG_AUTO_CANCEL;//点击后自动清除通知(Notification.FLAG_NO_CLEAR无法清除通知等等等) Intent intent = new Intent(); intent.setAction(Intent.ACTION_CALL); intent.setData(Uri.parse("tel:119"));//向119拨打电话,要增加打电话的权限 PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0); notification.setLatestEventInfo(this, "我是标题", "我是内容", contentIntent); nm.notify(0, notification); } /** * 新版本的notification * @param view */ @SuppressLint("NewApi") public void click2(View view){ Notification noti = new Notification.Builder(this) .setContentTitle("我是标题") .setContentText("我是内容") .setSmallIcon(R.drawable.notification) .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher)) .build(); NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); nm.notify(0, noti); }
时间: 2024-10-30 22:42:32