开发android的同学可能会抱怨Toast设定显示的时长无效,只能是Toast.LENGTH_LONG 或者Toast.LENGTH_SHORT 之一,为了解决这些办法,有多种实现方式:
1.使用定时器,定时调用show()方法.
2.使用CountDownTimer类,也是调用show()方法.
3.使用WindownManager类实现.
本文使用方法三进行实现,难度不大,直接看代码吧.
复制代码
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245package com.open.toast; import android.content.Context; import android.graphics.Color; import android.graphics.PixelFormat; import android.os.Handler; import android.view.Gravity; import android.view.View; import android.view.WindowManager; import android.widget.LinearLayout; import android.widget.TextView; /** * 自定义时长的Toast * @author DexYang * */ public class CToast { public static CToast makeText(Context context, CharSequence text, int duration) { CToast result = new CToast(context); LinearLayout mLayout=new LinearLayout(context); TextView tv = new TextView(context); tv.setText(text); tv.setTextColor(Color.WHITE); tv.setGravity(Gravity.CENTER); mLayout.setBackgroundResource(R.drawable.widget_toast_bg); int w=context.getResources().getDisplayMetrics().widthPixels / 2; int h=context.getResources().getDisplayMetrics().widthPixels / 10; mLayout.addView(tv, w, h); result.mNextView = mLayout; result.mDuration = duration; return result; } public static final int LENGTH_SHORT = 2000; public static final int LENGTH_LONG = 3500; private final Handler mHandler = new Handler(); private int mDuration=LENGTH_SHORT; private int mGravity = Gravity.CENTER; private int mX, mY; private float mHorizontalMargin; private float mVerticalMargin; private View mView; private View mNextView; private WindowManager mWM; private final WindowManager.LayoutParams mParams = new WindowManager.LayoutParams(); public CToast(Context context) { init(context); } /** * Set the view to show. * @see #getView */ public void setView(View view) { mNextView = view; } /** * Return the view. * @see #setView */ public View getView() { return mNextView; } /** * Set how long to show the view for. * @see #LENGTH_SHORT * @see #LENGTH_LONG */ public void setDuration(int duration) { mDuration = duration; } /** * Return the duration. * @see #setDuration */ public int getDuration() { return mDuration; } /** * Set the margins of the view. * * @param horizontalMargin The horizontal margin, in percentage of the * container width, between the container's edges and the * notification * @param verticalMargin The vertical margin, in percentage of the * container height, between the container's edges and the * notification */ public void setMargin(float horizontalMargin, float verticalMargin) { mHorizontalMargin = horizontalMargin; mVerticalMargin = verticalMargin; } /** * Return the horizontal margin. */ public float getHorizontalMargin() { return mHorizontalMargin; } /** * Return the vertical margin. */ public float getVerticalMargin() { return mVerticalMargin; } /** * Set the location at which the notification should appear on the screen. * @see android.view.Gravity * @see #getGravity */ public void setGravity(int gravity, int xOffset, int yOffset) { mGravity = gravity; mX = xOffset; mY = yOffset; } /** * Get the location at which the notification should appear on the screen. * @see android.view.Gravity * @see #getGravity */ public int getGravity() { return mGravity; } /** * Return the X offset in pixels to apply to the gravity's location. */ public int getXOffset() { return mX; } /** * Return the Y offset in pixels to apply to the gravity's location. */ public int getYOffset() { return mY; } /** * schedule handleShow into the right thread */ public void show() { mHandler.post(mShow); if(mDuration>0) { mHandler.postDelayed(mHide, mDuration); } } /** * schedule handleHide into the right thread */ public void hide() { mHandler.post(mHide); } private final Runnable mShow = new Runnable() { public void run() { handleShow(); } }; private final Runnable mHide = new Runnable() { public void run() { handleHide(); } }; private void init(Context context) { final WindowManager.LayoutParams params = mParams; params.height = WindowManager.LayoutParams.WRAP_CONTENT; params.width = WindowManager.LayoutParams.WRAP_CONTENT; params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON; params.format = PixelFormat.TRANSLUCENT; params.windowAnimations = android.R.style.Animation_Toast; params.type = WindowManager.LayoutParams.TYPE_TOAST; params.setTitle("Toast"); mWM = (WindowManager) context.getApplicationContext() .getSystemService(Context.WINDOW_SERVICE); } private void handleShow() { if (mView != mNextView) { // remove the old view if necessary handleHide(); mView = mNextView; // mWM = WindowManagerImpl.getDefault(); final int gravity = mGravity; mParams.gravity = gravity; if ((gravity & Gravity.HORIZONTAL_GRAVITY_MASK) == Gravity.FILL_HORIZONTAL) { mParams.horizontalWeight = 1.0f; } if ((gravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.FILL_VERTICAL) { mParams.verticalWeight = 1.0f; } mParams.x = mX; mParams.y = mY; mParams.verticalMargin = mVerticalMargin; mParams.horizontalMargin = mHorizontalMargin; if (mView.getParent() != null) { mWM.removeView(mView); } mWM.addView(mView, mParams); } } private void handleHide() { if (mView != null) { if (mView.getParent() != null) { mWM.removeView(mView); } mView = null; } } }
测试类的代码如下:
复制代码
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
57package com.open.toast; import android.app.Activity; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.widget.EditText; public class MainActivity extends Activity { private EditText mEditText; private CToast mCToast; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } private void init() { mEditText=(EditText)findViewById(R.id.timeEditText); findViewById(R.id.showToastBtn).setOnClickListener(listener); findViewById(R.id.hideToastBtn).setOnClickListener(listener); } private View.OnClickListener listener=new View.OnClickListener() { @Override public void onClick(View v) { switch(v.getId()) { case R.id.showToastBtn: if(null!=mCToast) { mCToast.hide(); } int time=TextUtils.isEmpty(mEditText.getText().toString())?CToast.LENGTH_SHORT:Integer.valueOf(mEditText.getText().toString()); mCToast=CToast.makeText(getApplicationContext(), "我来自CToast!",time); mCToast.show(); break; case R.id.hideToastBtn: if(null!=mCToast) { mCToast.hide(); } break; } } }; }
效果如下:
源码下载:android自定义Toast设定显示时间
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持靠谱客。
最后
以上就是跳跃银耳汤最近收集整理的关于android自定义Toast设定显示时间的全部内容,更多相关android自定义Toast设定显示时间内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复