«

快速掌握Android三个常用自定义控件Toast AlertDialog Notification

时间:2024-3-2 18:33     作者:韩俊     分类: Android


今天浏览到网上相关方法看起来有点复杂,于是总结了下安卓自定义Toast 自定义AlertDialog 自定义Notification:

//1

Toast toast;

public void myToast(){

if(toast!=null){

toast.cancel();//优化重复toast时消息排队时间过长

}

toast =new Toast(this);

View inflate =getLayoutInflater().inflate(R.layout.toast_item,null);

toast.setView(inflate);

toast.show();

}

//2

public void myAlertDialog (){

View view=getLayoutInflater().inflate(R.layout.dialog,null);//dialog自定义布局

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

builder.show();

builder.getWindow().setContentView(view);

}

//3

publicvoid myNotification () {

NotificationManagernotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

Notification status=new Notification();

status.contentView =new RemoteViews (getPackageName(), R.layout.custom); //自定义Notification远程控件RemoteViews

status.flags= Notification.FLAG_ONGOING_EVENT;

status.icon = R.drawable.ic_launcher;

status.tickerText="自定义通知";

status.contentIntent = PendingIntent.getActivity(this, 0,new Intent(this, MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

notificationManager.notify(0,status);

}

//注意:RemoteViews不支持SeekBar只支持类标识了@RemoteView的控件: http://blog.csdn.net/a704755096/article/details/46472703

标签: android

热门推荐