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

网上相关自定义方法很多,总结整理了下安卓自定义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   设置系统级dialog: setType(LayoutParams.TYPE_SYSTEM_ALERT);//建议type=2002

 

public void myAlertDialog(){
       View view=getLayoutInflater().inflate(R.layout.dialog,null);//dialog自定义布局
       AlertDialog dialog = new AlertDialog.Builder(this).show();
       Window window = dialog.getWindow();
       WindowManager.LayoutParams attributes = window.getAttributes();
       attributes.width=WindowManager.LayoutParams.MATCH_PARENT;//无间距铺满
       window.setAttributes(attributes);
       window.setBackgroundDrawableResource(android.R.color.transparent);//背景透明铺满
       window.setGravity(Gravity.BOTTOM);
       window.setContentView(view);//Builder.setCustomTitle();builder.setView();
       window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);//允许输入法
  /*隐藏输入法:1.android:windowSoftInputMode="adjustResize|stateAlwaysHidden" 
  *           2.inputMethodManager.hideSoftInputFromWindow(edittext.getWindowToken(),0);
  * 切换输入法:((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(0,0);
  */    
}

 

Notification

 

public void myNotification(){
NotificationManager nm=(NotificationManager)getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
                Intent intentgo=new Intent(Content,MainActivity.class);
                PendingIntent intent = PendingIntent.getActivity(Content,0,intentgo,0);
                Notification.Builder notify=new Notification.Builder(Content)
                      .setSmallIcon(R.drawable.ic_logo)
                      .setContentTitle("通知")
                      .setContentText("您有一条新的消息")
                      .setDefaults(Notification.DEFAULT_SOUND)
                      .setProgress(0,0,true)
                      .setAutoCancel(true)
                      .setContentIntent(intent);//intent不为空
//setContent(new RemoteViews(getPackageName(),R.layout.xx))
                if (Build.VERSION.SDK_INT >= 26) {
                    notify.setChannelId("channel");
                    nm.createNotificationChannel(new NotificationChannel("channel","消息",NotificationManager.IMPORTANCE_DEFAULT));
                }
                nm.notify(1, notify.build());//刷新
}

 

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

http://blog.csdn.net/a704755096/article/details/46472703

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值