欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android編程實(shí)現(xiàn)Toast只顯示最后一條的方法

 更新時(shí)間:2017年08月31日 11:23:27   作者:QQxiaoqiang1573  
這篇文章主要介紹了Android編程實(shí)現(xiàn)Toast只顯示最后一條的方法,結(jié)合實(shí)例形式總結(jié)了Toast只顯示最后一條的原理與具體實(shí)現(xiàn)技巧,需要的朋友可以參考下

本文實(shí)例講述了Android編程實(shí)現(xiàn)Toast只顯示最后一條的方法。分享給大家供大家參考,具體如下:

在做Android開(kāi)發(fā)中,時(shí)不時(shí)的可能會(huì)用到Toast,但用Toast的時(shí)候,連續(xù)使用會(huì)存在一個(gè)問(wèn)題,就是一條條顯示Toast。而不是直接顯示最后一條。因此,根據(jù)此需求,現(xiàn)在寫(xiě)了ToastUtil這個(gè)類(lèi),該類(lèi)中有三個(gè)方法供選擇。

ToastUtil.Java

import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.Gravity;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;
public class ToastUtil {
  //方法一
  private static Handler mHandler = new Handler(Looper.getMainLooper());
  private static Toast mToast = null;
  private static Object synObject = new Object();
  public static void showToastByThread(Context context, String msg){
    showToastByThread(context, msg, Toast.LENGTH_SHORT);
  }
  public static void showToastByThread(Context context, int msg){
    showToastByThread(context, context.getText(msg), Toast.LENGTH_SHORT);
  }
  public static void showToastByThread(final Context context, final CharSequence msg, final int length){
    new Thread(new Runnable() {
      @Override
      public void run() {
        mHandler.post(new Runnable() {
          @Override
          public void run() {
            synchronized (synObject) {
              if (mToast != null){
                mToast.setText(msg);
                mToast.setDuration(length);
              }else{
                mToast = Toast.makeText(context, msg, length);
              }
              mToast.show();
            }
          }
        });
      }
    }).start();
  }
  //方法二:注意此方法不能再子線(xiàn)程中使用
  private static long oneTime;
  private static long twoTime;
  private static String oldMsg;
  public static void showToastByTime(Context context, String msg){
    if (mToast == null) {
      mToast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
      mToast.show();
      oneTime = System.currentTimeMillis();
    } else {
      twoTime = System.currentTimeMillis();
      if (msg.equals(oldMsg)){
        if (twoTime-oneTime > Toast.LENGTH_SHORT){
          mToast.show();
        }
      } else {
        oldMsg = msg;
        mToast.setText(msg);
        mToast.show();
      }
    }
    oneTime = twoTime;
  }
  public static void showToastByTime(Context context, int msg){
    showToastByTime(context, context.getString(msg));
  }
  //方法三
  public static TextView mTextView;
  public static WindowManager mWindowManager = null;
  private static Handler mPriHandler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
      cancelToast();
    }
  };
  public static void showToastByWindow(Context context, String msg){
    mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    if (mTextView == null){
      mTextView = new TextView(context);
    }
    mTextView.setText(msg);
    mTextView.setTextSize(20);
    mTextView.setPadding(0, 0, 0, 30);
    if (mTextView.getParent() == null){
      WindowManager.LayoutParams params = new WindowManager.LayoutParams();
      params.gravity= Gravity.BOTTOM;
      params.alpha = 0.65f;
      params.x = 0;
      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 |
          WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
      params.format = PixelFormat.TRANSLUCENT;
      params.windowAnimations = 0;
      mWindowManager.addView(mTextView, params);
      mPriHandler.sendEmptyMessageDelayed(101, 1000);
    } else {
      mTextView.setText(msg);
      mPriHandler.removeMessages(101);
      mPriHandler.sendEmptyMessageDelayed(101, 1000);
    }
  }
  public static void cancelToast(){
    if (mTextView != null && mTextView.getParent() != null){
      mWindowManager.removeView(mTextView);
    }
  }
}

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Android布局layout技巧總結(jié)》、《Android開(kāi)發(fā)入門(mén)與進(jìn)階教程》、《Android調(diào)試技巧與常見(jiàn)問(wèn)題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》及《Android控件用法總結(jié)

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論