当前位置: 代码迷 >> Android >> 高速掌握Android三个常用自定义控件Toast AlertDialog Notification
  详细解决方案

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

热度:373   发布时间:2016-04-28 00:27:25.0
快速掌握Android三个常用自定义控件Toast AlertDialog Notification

今天浏览到网上相关方法看起来有点复杂,于是总结了下安卓自定义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远程控件

        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);

  }

//Notification尽量不使用SeekBar: http://blog.csdn.net/a704755096/article/details/46472703


  相关解决方案