您的位置:首页 > 运维架构

Toast,Dialog,PopupWindow,Notification

2015-09-04 17:21 393 查看
1.Toast 依赖应用而不以来Activity,一般用getAppliacationContent。

构造器一般传三个参数。可单独设置setText,可以传入富文本。setDuration,设置显示的时长。setGravity(Gravity.CENTER(LEFT等),X偏移量int,Y偏移量int),控制Toast的位置。

可以自己写Layout,自定义Toast,setView。

2.Dialog AlertDialog

A、最简单的dialog

AlertDialog.Builder builder=new AlertDialog.Builder(….this);

builder.setICon,setTitle,setMessage,setNegativeButton,setNeutralButton,setPositiveButton…….

AlertDialog dialog=builder.create(); dialog.show();

B、弹列表框

setItems(数据数组,new Dialog….Click)

C、弹单选框

setSingleChoiceItems(数据数组,0,new Dialog…Click) 0为默认选中的

D、弹多选框

setMultichoiceItems(数据数组,boolean数组,new Dialog…OnMultichoice….) boolean接受是否checked

E、自定义Dialog

Dialog dialog=new Dialog(Ma..this,可加style),setContentView,requestWindowFeature(Window.FEATURE.NOTITLE)去掉标题栏,

F、DatePickerDialog 构造器(..this,new …OnDateSetListener..,mCalendar.get(YEAR),MONTH,DAY),5参数

G、TimePickerDialog 构造器(…this,new OnTimeSetListener…,mCalendar.get(HOUR24小时制),MINUTE,true24小时制) 5参数

3.Drawable中加< shape corners可设置角的圆滑度,solid可设置背景色,stroke可设置边框颜色和宽度

4.Theme一般指整体及系统的主题,style指某一块(button,dialog等控件)的风格(color,padding,size等),将View的样式属性可写到sytle.xml中,在控件最后一行加style=@style….

5.PopupWindow 在某控件底部或..弹出框,必须自己写View

构造器(….this),setWidth(ViewGroup.LayoutParams.MATCH.PARENT),setHeight,setContentView,setFocusable,SetOutSideTouchable(设置点击其他位置后消失),showAsDropDown(控件)

6.onKeyDown方法,if(keycode==keyEvent.KEY….BACK)如果按下返回键,可进行自定义操作

7.Notification PendingIntent(持有Intent),也可以说Intent的包装,在特定时刻触发。而Intent是及时传送。先声明NotificationManager=…getSystemService(NOTIFACATION SERVICE)

旧版本:

Notification notification=new Notification();//初始化Notification
notification.icon=R.mipmap.ic_launcher;//设置通知的图片
notification.tickerText="我是一个消息";//设置状态栏消息
notification.flags=Notification.FLAG_AUTO_CANCEL;//设置为可以取消通知
Intent intent=new Intent(getApplicationContext(),MainActivity.class);
//设置Intent供PendingIntent持有
PendingIntent pendingIntent=PendingIntent.getActivity(getApplicationContext(), 1, intent, PendingIntent.FLAG_ONE_SHOT);
//设置PendingIntent使用方式,Intent对象的包装
notification.setLatestEventInfo(getApplicationContext(),"我是标题","我是内容",pendingIntent);
//设置通知的内容和PendingIntent
notification.when=System.currentTimeMillis();//Calendar.getInstance().getTimeInMillis();设置触发时间
mNotificationManager.notify(1,notification);//通知管理器添加通知,id=1


新版本

Intent intent1=new Intent(getApplicationContext(),MainActivity.class);
PendingIntent pendingIntent1=PendingIntent.getActivity(getApplicationContext(),1,intent1,PendingIntent.FLAG_ONE_SHOT);
Notification notification1=new Notification.Builder(getApplicationContext()).setSmallIcon(R.mipmap.ic_launcher).
setTicker("状态栏显示消息").setContentTitle("消息题目").setContentInfo("时间下面").
setContentText("我是内容").setWhen(System.currentTimeMillis()).
setContentIntent(pendingIntent1).setAutoCancel(true).build();
mNotificationManager.notify(2,notification1);


8.自定义Notification

RemoteView 只可以加简单的控件,TextView,ImageView,ProgressBar

RmoteViews rv=new RemoteViews(getPackageName(),R.layout.自定义layout)

Notification.Builder.setContent(rv)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: