服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - android通过自定义toast实现悬浮通知效果的示例代码

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

2022-08-10 11:15赖床的猫 Android

这篇文章主要介绍了android通过自定义toast实现悬浮通知效果,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

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

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

实现的功能:

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

代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
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布局:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?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中需要初始化一次:

?
1
PushToast.getInstance().init(this);

调用:

?
1
PushToast.getInstance().createToast(msg.title,msg.text,umengPushEntity.getExtraMap());

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

原文链接:https://www.jianshu.com/p/448abea48e74

延伸 · 阅读

精彩推荐