码迷,mamicode.com
首页 > 移动开发 > 详细

Android开发之《实现类似Toast可以自动消失的提示栏Tip》

时间:2017-05-21 17:51:18      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:new t   highlight   efault   man   simple   name   widget   java   purge   

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);
    }
}

  

Android开发之《实现类似Toast可以自动消失的提示栏Tip》

标签:new t   highlight   efault   man   simple   name   widget   java   purge   

原文地址:http://www.cnblogs.com/alanfang/p/6885235.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!