开发android的同学可能会抱怨Toast设定显示的时长无效,只能是Toast.LENGTH_LONG 或者Toast.LENGTH_SHORT 之一,为了解决这些办法,有多种实现方式:

1.使用定时器,定时调用show()方法.

2.使用CountDownTimer类,也是调用show()方法.

3.使用WindownManager类实现.

本文使用方法三进行实现,难度不大,直接看代码吧.

package 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;

}

}

}

测试类的代码如下:

package 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设定显示时间相关推荐

  1. qt5设置linux系统时间,Qt中使用QLabel显示时间的两种方法

    Qt中使用QLabel显示时间的两种方法思路一致,只是实现方法不一样而已. main.cpp #include "displaytime.h" #include int main( ...

  2. android设置系统锁屏时间或屏保显示时间

    public static final String SCREEN_OFF_TIMEOUT = "screen_off_timeout";private final int SCR ...

  3. android toast 自定义时间,Android Toast自定义显示时间

    Toast是Android中使用频率较高的弹窗提示手段,使用起来简单.方便.常规使用方法这里不做说明,继前一篇博客<Android中Toast全屏显示> ,其中抛砖引玉的给出一个简单的实现 ...

  4. 华为android系统显示时间,华为nova2s怎么在桌面显示时间天气 | 手游网游页游攻略大全...

    发布时间:2016-05-06 怎么看时间?相信不少玩家还不是很了解,不用着急,今天小编带来"yjqjake"分享的显示时间操作方法,希望对各位有帮助. 小编推荐修改器使用小技巧& ...

  5. html css 水平时间轴,纯css+js水平时间轴

    自定义,并自动加载时间节点 当前时间节点居中,突出显示 时间动态无痕添加 效果图: 初始状态 时间左走到一定2016.1月后 html: + - 对应 JS 设置处理: var left = docu ...

  6. html语言中 加当前时间,javascript中怎么获取当前时间?

    在web开发中,通过js获取时间非常的常用.下面本篇文章就来给大家介绍一下使用javascript获取当前时间的方法,希望对大家有所帮助. 在javascript中可以使用Date对象中的Date() ...

  7. Android 自定义带图标Toast,工具方法,Toast自定义显示时间

    带图标Toast工具方法1 样式 <?xml version="1.0" encoding="utf-8"?> <shape xmlns:an ...

  8. javascript获取系统时间时区_javascript怎么获取当前时间?

    javascript怎么获取当前时间?下面本篇文章就来给大家介绍一下使用javascript获取当前时间的方法,希望对大家有所帮助. 在JavaScript中可以使用Date对象中的Date()方法来 ...

  9. 获取进入html页面的时间,javascript中怎么获取当前时间?

    在web开发中,通过js获取时间非常的常用.下面本篇文章就来给大家介绍一下使用javascript获取当前时间的方法,希望对大家有所帮助. 在javascript中可以使用Date对象中的Date() ...

最新文章

  1. 匹配算法:局部结构保留
  2. 安卓收藏功能怎么实现_收藏!微信群接龙功能怎么操作?
  3. 2012年生活方向盘
  4. android 通过GPS获取用户地理位置并监听位置变化
  5. 嵌入式web服务器预研报告(转)
  6. 电气毕业生在国家电网都干啥工作?
  7. thinkPHP5.0数据查询表达式生成技巧
  8. Linux系统编程3:基础篇之详解Linux软件包管理器yum
  9. Windows 网络无法ping通的解决方法
  10. 毕业答辩之毕业设计答辩问题有哪些?
  11. 新浪微博开放平台链接耗尽的情况分析
  12. AWK相关学习(转)
  13. openproj jvm erron 193
  14. SQL server 还原数据库遇到正在使用的解决方法:
  15. 泊松过程2 | 泊松过程扩展
  16. 卡内基·梅隆大学计算机科学系主任周以真的父母是中国人吗,清华大学计算机科学与技术系...
  17. 程序员跳槽频繁不稳定被质疑,一番话后HR哑口无言!
  18. 终极dos批处理循环命令详解
  19. 10条不可不知的手机礼仪 看看你犯过哪几项?
  20. VS2019安装教程(C语言)

热门文章

  1. Bailian2797 最短前缀【字典树】
  2. POJ1029 UVA158 Calendar题解
  3. Bailian3659 判断是否为C语言的合法标识符【文本处理】
  4. TFRecord —— tensorflow 下的统一数据存储格式
  5. 计算机的组成 —— 存储(内存/硬盘)
  6. MXNet 的学习(一)—— MXNet Dependency Engine(依赖引擎)
  7. mos 控制交流_MOS管和IGBT管的区别
  8. nginx websocket wss 连接失败 failed_浅谈WebSocket协议、WS协议和WSS协议原理及关系
  9. python线程监控_Python监控php-fpm进程
  10. python在线学习直播-Python在线学习最有效马哥开启全网独家全程直播课