首页 > 代码库 > Android开发之《实现类似Toast可以自动消失的提示栏Tip》
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); } }
Android开发之《实现类似Toast可以自动消失的提示栏Tip》
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。