标签:
//创建警告框对象
AlertDialog.Builder ad = new Builder(this);
//给对象添加标题
ad.setTitle("hey");
//添加显示的信息
ad.setMessage("hello guys");
//添加确定按钮
ad.setPositiveButton("確定", new OnClickListener() {
//点击确认时弹出土司
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "確定咯~~~~~", 0).show();
}
});
//添加取消按钮
ad.setNegativeButton("取消", new OnClickListener() {
//点击取消时,弹出吐司
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "取消咯~~~~~", 0).show();
}
});
//对话框显示在界面上
ad.show();
//创建警告框对象
AlertDialog.Builder ad = new Builder(this);
//添加属性
final String[] items = new String[] { "男", "女", "你猜" };
//添加单选选项
ad.setSingleChoiceItems(items, 0, new OnClickListener() {
//点击时弹出土司
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "性別: " + items[which],
Toast.LENGTH_SHORT).show();
//选择后关闭单选对话框
dialog.dismiss();
}
});
//将对话框显示在界面上
ad.show();
//建立一个对话框对象
AlertDialog.Builder ad = new Builder(this);
//设置选项的值
final String[] items = new String[]{"草莓","橘子","菠萝","苹果","香蕉"};
//默认都不被选中
final boolean[] checkedItems = new boolean[]{false,false,false,false,false};
final StringBuilder sb = new StringBuilder();
//添加多选选项
ad.setMultiChoiceItems(items, checkedItems, new OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
//哪项被选中后将值付给checkedItems
checkedItems[which] = isChecked;
}
});
//添加确定按钮
ad.setPositiveButton("选择完毕", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
for(int i = 0;i<checkedItems.length;i++){
//如果 哪一个选项被选中了就把值取出来保存进sb中
if(checkedItems[i]==true){
String food = items[i];
sb.append(food+" ");
}
}
Toast.makeText(MainActivity.this, "喜欢吃的水果有:"+ sb.toString(), Toast.LENGTH_SHORT).show();
//关闭对话框
dialog.dismiss();
}
});
//将对话框显示界面上
ad.show();
(pd.dismiss()方法可以被子线程调用)
//创建进度对话框的对象
final ProgressDialog pd = new ProgressDialog(this);
//给进度条添加标题
pd.setTitle("正在刷新");
//给进度条添加信息
pd.setMessage("正在加载,请稍后");
new Thread(){
public void run(){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//关闭进度对话框
pd.dismiss();
}
}.start();
//把对话框显示在界面上
pd.show();
就是比进度多2条设置
//将进度对话框的风格设置为横向的 pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); //设置进度条对话框的最大值 pd.setMax(100); 将进度值添加到进度条中
代码
//创建进度对话框的对象
final ProgressDialog pd = new ProgressDialog(this);
//将进度对话框的风格设置为横向的
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//设置进度条对话框的最大值
pd.setMax(100);
//给进度条添加标题
pd.setTitle("正在刷新");
//给进度条添加信息
pd.setMessage("正在加载,请稍后");
new Thread(){
public void run(){
try {
for(int i = 0 ;i <=3 ;i ++){
Thread.sleep(500);
pd.setProgress(i*33);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
//关闭进度对话框
pd.dismiss();
}
}.start();
//把对话框显示在界面上
pd.show();
Toast和对话框是显示在应用程序中
通知提醒是显示在桌面的状态栏
//通过服务获取通知管理者对象
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//创建通知
Notification noti = new Notification.Builder(this)
//设置通知的标题
.setContentTitle("注意")
//设置通知的内容
.setContentText("消息来了")
//设置小图标
.setSmallIcon(R.drawable.ic_launcher)
//设置大图标
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
//配置生效
.build();
//设置标记为不可清楚
noti.flags = Notification.FLAG_NO_CLEAR;
//利用通知管理者对象发布通知
//第一个属性是设置消息的ID,如果2条消息的id值相同,则第二条会取代第一条消息
nm.notify(0, noti);
//通过服务获取通知管理者对象
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//创建通知消息
Notification noti = new Notification(R.drawable.ic_launcher, "通知来了", System.currentTimeMillis());
// 设置标记为不可清除
noti.flags = Notification.FLAG_NO_CLEAR;
//创建隐式意图
Intent intent = new Intent();
//设置动作,打开拨号盘
intent.setAction(Intent.ACTION_DIAL);
//设置电话号码为110
intent.setData(Uri.parse("tel://110"));
//设置另外一个程序执行的意图
PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
//点击通知时会打开pendingIntent意图
noti.setLatestEventInfo(this, "注意咯~", "发送成功鸟~~~~", pendingIntent);
//利用通知管理者对象发布通知
//第一个属性是设置消息的ID,如果2条消息的id值相同,则第二条会取代第一条消息
nm.notify(2, noti);
注意
//创建隐式意图 Intent intent = new Intent(); //设置动作,打开拨号盘 intent.setAction(Intent.ACTION_DIAL); //设置电话号码为110 intent.setData(Uri.parse("tel://110")); //设置另外一个程序执行的意图 PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT); //点击通知时会打开pendingIntent意图 noti.setLatestEventInfo(this, "注意咯~", "发送成功鸟~~~~", pendingIntent); //利用通知管理者对象发布通知 //第一个属性是设置消息的ID,如果2条消息的id值相同,则第二条会取代第一条消息 nm.notify(2, noti);上面的代码是当用户收到通知时,点击通知信息后,跳转到系统的拨号盘,并且拨号盘的内容为110
取消所有的通知
NotificationManager nm =(NotificationManager) getSystemService(NOTIFICATION_SERVICE); nm.cancelAll();
取消指定的通知
NotificationManager nm =(NotificationManager) getSystemService(NOTIFICATION_SERVICE); //这里的id是上面发送消息时指定的id nm.cancel(1);
标签:
原文地址:http://www.cnblogs.com/fengchuxiaodai/p/5322755.html