欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android通过自定义toast实现悬浮通知效果的示例代码

程序员文章站 2024-02-10 15:18:16
android通过toast实现悬浮通知效果,如图: 实现的功能:  自定义悬浮弹窗; 点击其他地方该布局不受影响; 可自定义显示时间...

android通过toast实现悬浮通知效果,如图:

android通过自定义toast实现悬浮通知效果的示例代码

实现的功能:

  •  自定义悬浮弹窗;
  • 点击其他地方该布局不受影响;
  • 可自定义显示时间;
  • 可以设置点击事件;

代码如下:

import android.content.context;
import android.os.build;
import android.os.handler;
import android.os.message;
import android.support.v7.app.appcompatactivity;
import android.view.gravity;
import android.view.layoutinflater;
import android.view.view;
import android.view.windowmanager;
import android.widget.linearlayout;
import android.widget.textview;
import android.widget.toast;
import java.lang.reflect.field;
import java.lang.reflect.invocationtargetexception;
import java.lang.reflect.method;
import java.util.map;
import cn.droidlover.xdroidmvp.router.router;
import io.slife.wallet.r;
import io.slife.wallet.config.intentkey;
import io.slife.wallet.ui.newsflashdetailactivity;

public class pushtoast {
private appcompatactivity mactivity;
private static pushtoast minstance;
private toast mtoast;
private final int show = 1;
private final int hide = 0;
private object mtn;
private method mshow;
private method mhide;
private field mviewfeild;
private long durationtime = 5*1000;

public static pushtoast getinstance() {
 if (minstance == null) {
  minstance = new pushtoast();
 }
 return minstance;
}

public void init(appcompatactivity activity) {
 mactivity = activity;
}

public void createtoast(string title, string content, map<string,string> params) {
 if (mactivity == null) {
  return;
 }
 layoutinflater inflater = mactivity.getlayoutinflater();//调用activity的getlayoutinflater()
//  layoutinflater inflater = (layoutinflater) context.getapplicationcontext().getsystemservice(context.layout_inflater_service);
 view view = inflater.inflate(r.layout.view_push_toast, null); //加載layout下的布局
 linearlayout llpushcontent = (linearlayout) view.findviewbyid(r.id.ll_push_content);
 textview tvtitle = (textview) view.findviewbyid(r.id.tv_title);
 textview tvcontent = (textview) view.findviewbyid(r.id.tv_content);
 tvtitle.settext(title);
 tvcontent.settext(content);
 mtoast = new toast(mactivity);
 mtoast.setview(view);
 mtoast.setduration(toast.length_long);
 mtoast.setgravity(gravity.top, 0, 0);
 reflectenableclick();
 reflecttoast();
 llpushcontent.setonclicklistener(new view.onclicklistener() {
  @override
  public void onclick(view v) {
   string newsflashid = params.get("informationid");
   router.newintent(mactivity).to(newsflashdetailactivity.class).putstring(intentkey.news_flash_id,newsflashid).launch();
   handler.sendemptymessage(hide);
  }
 });
 if(mshow != null && mhide != null){
  handler.sendemptymessage(show);
 }else{
  mtoast.show();
 }
}

private void reflectenableclick() {
 try {
  object mtn;
  mtn = getfield(mtoast, "mtn");
  if (mtn != null) {
   object mparams = getfield(mtn, "mparams");
   if (mparams != null
     && mparams instanceof windowmanager.layoutparams) {
    windowmanager.layoutparams params = (windowmanager.layoutparams) mparams;
    //显示与隐藏动画
//     params.windowanimations = r.style.clicktoast;
    //toast可点击
    params.flags = windowmanager.layoutparams.flag_keep_screen_on
      | windowmanager.layoutparams.flag_not_focusable;
    //设置viewgroup宽高
    params.width = windowmanager.layoutparams.match_parent; //设置toast宽度为屏幕宽度
    params.height = windowmanager.layoutparams.wrap_content; //设置高度
   }
  }
 } catch (exception e) {
  e.printstacktrace();
 }
}

/**
 * 反射字段
 *
 * @param object 要反射的对象
 * @param fieldname 要反射的字段名称
 */
private static object getfield(object object, string fieldname)
  throws nosuchfieldexception, illegalaccessexception {
 field field = object.getclass().getdeclaredfield(fieldname);
 if (field != null) {
  field.setaccessible(true);
  return field.get(object);
 }
 return null;
}

private handler handler = new handler() {
 @override
 public void handlemessage(message msg) {
  super.handlemessage(msg);
  switch (msg.what) {
   case show:
    handler.sendemptymessagedelayed(hide, durationtime);
    show();
    break;
   case hide:
    hide();
    break;
  }
 }
};

public void reflecttoast() {
 field field = null;
 try {
  field = mtoast.getclass().getdeclaredfield("mtn");
  field.setaccessible(true);
  mtn = field.get(mtoast);
  mshow = mtn.getclass().getdeclaredmethod("show");
  mhide = mtn.getclass().getdeclaredmethod("hide");
  mviewfeild = mtn.getclass().getdeclaredfield("mnextview");
  mviewfeild.setaccessible(true);
 } catch (nosuchfieldexception e) {
  e.printstacktrace();
 } catch (illegalaccessexception e) {
  e.printstacktrace();
 } catch (illegalargumentexception e) {
  e.printstacktrace();
 } catch (nosuchmethodexception e1) {
  e1.printstacktrace();
 }
}

public void show() {
 try {
  //android4.0以上就要以下处理
  if (build.version.sdk_int > 14) {
   field mnextviewfield = mtn.getclass().getdeclaredfield("mnextview");
   mnextviewfield.setaccessible(true);
   layoutinflater inflate = (layoutinflater) mactivity.getsystemservice(context.layout_inflater_service);
   view v = mtoast.getview();
   mnextviewfield.set(mtn, v);
   method method = mtn.getclass().getdeclaredmethod("show", null);
   method.invoke(mtn, null);
  }
  mshow.invoke(mtn, null);
 } catch (exception e) {
  e.printstacktrace();
 }
}

private void hide() {
 try {
  mhide.invoke(mtn, null);
 } catch (illegalaccessexception e) {
  e.printstacktrace();
 } catch (illegalargumentexception e) {
  e.printstacktrace();
 } catch (invocationtargetexception e) {
  e.printstacktrace();
 }catch (nullpointerexception ex){
  ex.printstacktrace();
 }
}
}

xml布局:

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_push_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_push_message"
android:orientation="vertical">
<textview
 android:id="@+id/tv_title"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:textcolor="@color/black"
 android:text="标题"
 android:maxlines="2"
 android:ellipsize="end"
 android:textstyle="bold"
 android:textsize="@dimen/text_size_14" />
<textview
 android:id="@+id/tv_content"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:textcolor="@color/black_808080"
 android:textsize="@dimen/text_size_13"
 android:maxlines="3"
 android:ellipsize="end"
 android:layout_margintop="@dimen/margin_large"
 android:text="1"/>

</linearlayout>

点九格式图片:

android通过自定义toast实现悬浮通知效果的示例代码

使用方法:

activity中需要初始化一次:

pushtoast.getinstance().init(this);

调用:

pushtoast.getinstance().createtoast(msg.title,msg.text,umengpushentity.getextramap());

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。