Android札记-对话框提示
Android笔记-对话框提示
0. 土司提示方式: Toast toast = Toast.makeText(MsgSenderActivity.this, R.string.success, Toast.LENGTH_LONG); //创建土司提示对象, 第一个参数,需要显示的Activity对象, 第二个参数: 需要显示的内容, 第三个参数:需要显示的时间长度 toast.setMargin(RESULT_CANCELED, 0.345f); //设置提示对象显示的位置。 第一个参数: 显示的 横向, 第二个参数 显示的纵向 toast.show(); 1. 双选对话框: new AlertDialog.Builder(this) //此处 this 代表当前Activity .setTitle("baidu首页") .setCancelable(false) //设置不能通过“后退”按钮关闭对话框 .setMessage("浏览百度搜索?") .setPositiveButton("确认", new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialoginterface, int i){ Uri uri = Uri.parse("http://www.baidu.com/");//打开链接 Intent intent = new Intent(Intent.ACTION_VIEW, uri); startActivity(intent); } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }) .show();//显示对话框 2. 多个选项(单选) final String[] items = {"java", ".net", "php"}; new AlertDialog.Builder(this) .setTitle("选择语言") //此处 this 代表当前Activity .setItems(items, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int item) { Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show(); //将选中的文本内容按照土司提示 方式显示出来, 此处的getApplicationContext() 得到的也是当前的Activity对象,可用当前Activity对象的名字.this代替(Activity.this) } }).show();//显示对话框 //带单选框(详细解释,见上方不带单选框的多选功能) final String[] items = {"java", ".net", "php"}; new AlertDialog.Builder(this) .setTitle("选择语言") .setSingleChoiceItems(items, 1, new DialogInterface.OnClickListener() { //此处数字为选项的下标,从0开始, 表示默认哪项被选中 public void onClick(DialogInterface dialog, int item) { Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show(); dialog.cancel(); } }).show();//显示对话框 3. 多选提示框: final String[] items = {"java", ".net", "php"}; new AlertDialog.Builder(this) //此处 this 代表当前Activity .setCancelable(true) //设置不可撤销 .setTitle("选择语言") //设置标题 .setMultiChoiceItems(items, new boolean[]{false,true,false}, new DialogInterface.OnMultiChoiceClickListener() { //第一个参数:选项数组; 2: 默认被选正的项为true; 3.选中事件 @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { if(isChecked){ Toast.makeText(getApplicationContext(), items[which], // 选中后以土司方式提示被现实的 选项内容 which:被选中的下标 Toast.LENGTH_SHORT).show(); } } }) .setPositiveButton("确认", //显示确定按钮,点击后执行下面代码 new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialoginterface, int i){ dialoginterface.dismiss(); //关闭对话框 } }) .show();//显示对话框 4. 状态栏通知: NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); //创建通知管理对象。 getSystemService():根据名称获得系统服务对象。 Context.NOTIFICATION_SERVICE: 通知服务 int icon = android.R.drawable.stat_notify_chat; // 定义图标 long when = System.currentTimeMillis(); // 获取当前时间 //新建一个通知,指定其图标和标题 Notification notification = new Notification(icon, null, when);//创建一个通知。第一个参数为图标,第二个参数为标题,第三个为通知时间 notification.defaults = Notification.DEFAULT_SOUND;//设置通知发出的默认声音 Intent openintent = new Intent(this, this.class); //定义一个意图。 此处 this 代表当前Activity PendingIntent contentIntent = PendingIntent.getActivity(this, 0, openintent, 0);//当点击消息时就会向系统发送openintent意图 notification.setLatestEventInfo(this, "标题", "我是内容", contentIntent); //给通知设置最新的事件信息 mNotificationManager.notify(0, notification); //添加通知,第一个参数为自定义的通知唯一标识