zoukankan      html  css  js  c++  java
  • Android开发之《实现类似Toast可以自动消失的提示栏Tip》

    import java.util.Timer;
    import java.util.TimerTask;
    
    import android.app.Activity;
    import android.content.Context;
    import android.view.View;
    import android.widget.TextView;
    
    public class TipManager {
        private static final String TAG = TipManager.class.getSimpleName();
    
        private Context mContext;
        private TextView mView;
    
        private Timer timer = null;
        private TimerTask cancelTask = null;
    
        public TipManager(Context context, TextView view) {
            mContext = context;
            mView = view;
        }
    
        public void init() {
            timer = new Timer();
        }
    
        public void release() {
            if (cancelTask != null) {
                cancelTask.cancel();
                cancelTask = null;
            }
    
            if (timer != null) {
                timer.cancel();
                timer = null;
            }
        }
    
        public void show(CharSequence text) {
            // cancel hide task
            if (cancelTask != null) {
                cancelTask.cancel();
                cancelTask = null;
                timer.purge();
            }
    
            showUI(text);
        }
    
        public void show(CharSequence text, long duration) {
            showUI(text);
    
            // cancel hide task
            if (cancelTask != null) {
                cancelTask.cancel();
                cancelTask = null;
                timer.purge();
            }
    
            cancelTask = new TimerTask() {
                @Override
                public void run() {
                    hideUI("default");
                }
            };
    
            timer.schedule(cancelTask, duration);
        }
    
        private void showUI(final CharSequence text) {
            ((Activity) mContext).runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    doShow(text);
                }
            });
        }
    
        private void hideUI(final CharSequence text) {
            ((Activity) mContext).runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    doHide(text);
                }
            });
        }
    
        private void doShow(CharSequence text) {
            mView.setText(text);
            mView.setVisibility(View.VISIBLE);
        }
    
        private void doHide(CharSequence text) {
            mView.setText(text);
            mView.setVisibility(View.INVISIBLE);
        }
    }
    

      

  • 相关阅读:
    scrum
    control.begininvoke
    ChangeBrowsePosition Method
    常见linux命令(表格分类)
    Python 之优先级排序
    Python 之分辨双胞胎:copy(浅拷贝)与 deepcopy(深拷贝)
    字符编码学习总结
    Python 多继承方式及顺序
    AttributeError: module 'datetime' has no attribute 'now' ------解决方法之一
    Python 模块定义、导入、优化详解
  • 原文地址:https://www.cnblogs.com/alanfang/p/6885235.html
Copyright © 2011-2022 走看看