您的位置:首页 > 移动开发 > Android开发

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

2015-06-12 15:59 155 查看
网上相关自定义方法很多,总结整理了下安卓自定义Toast AlertDialog Notification:

Toast toast;

public void myToast(Context context,String text){
if(toast==null){
toast=Toast.makeText(context, text, Toast.LENGTH_SHORT);//或自定义Toast toast=new Toast(this);toast.setView(view);
toast.show();
}else{
toast.setText(text);
toast.show();
}
}


AlertDialog

public void myAlertDialog (){
View view=getLayoutInflater().inflate(R.layout.dialog,null);//dialog自定义布局
AlertDialog.Builder builder =new AlertDialog.Builder(this);
AlertDialog show = builder.show();
show.getWindow().setContentView(view);//设置大小show.getWindow.setLayout(200,200);
show.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
}


Notification

public void myNotification () {
NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.custom);

PendingIntent intent = PendingIntent.getActivity(context, 0, intent2, 0);

Notification notification = new Notification();

notification.icon =R.drawable.ic_launcher;//图标

notification.tickerText = "通知";//标题

notification.contentView = views;//通知自定义布局

notification.flags=Notification.FLAG_NO_CLEAR;//点击不消失;

notification.contentIntent = intent;//点击意图不能null

nm.notify(1, notification);
}


//注意:RemoteViews不支持SeekBar
只支持类标识了@RemoteView的控件

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