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

android自定義Toast設(shè)定顯示時間

 更新時間:2018年08月28日 09:53:10   作者:listen_to_heart  
這篇文章主要為大家詳細介紹了android自定義Toast設(shè)定顯示時間,具有一定的參考價值,感興趣的小伙伴們可以參考一下

開發(fā)android的同學(xué)可能會抱怨Toast設(shè)定顯示的時長無效,只能是Toast.LENGTH_LONG 或者Toast.LENGTH_SHORT 之一,為了解決這些辦法,有多種實現(xiàn)方式:

1.使用定時器,定時調(diào)用show()方法.

2.使用CountDownTimer類,也是調(diào)用show()方法.

3.使用WindownManager類實現(xiàn).

本文使用方法三進行實現(xiàn),難度不大,直接看代碼吧.

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設(shè)定顯示時間

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android開發(fā)之DatePickerDialog、TimePickerDialog時間日期對話框用法示例

    Android開發(fā)之DatePickerDialog、TimePickerDialog時間日期對話框用法示例

    這篇文章主要介紹了Android開發(fā)之DatePickerDialog、TimePickerDialog時間日期對話框用法,結(jié)合實例形式分析了Android使用DatePickerDialog、TimePickerDialog顯示日期時間相關(guān)操作技巧,需要的朋友可以參考下
    2019-03-03
  • Android LinearLayout實現(xiàn)自動換行效果

    Android LinearLayout實現(xiàn)自動換行效果

    這篇文章主要為大家詳細介紹了Android LinearLayout實現(xiàn)自動換行效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • Android ContentProvider基礎(chǔ)應(yīng)用詳解

    Android ContentProvider基礎(chǔ)應(yīng)用詳解

    ContentProvider是android四大組件之一。它是不同應(yīng)用程序之間交換數(shù)據(jù)的標準api,ContentProvider以某種uri的形式對外提供數(shù)據(jù),允許其它應(yīng)用程序?qū)ζ湓L問或者修改數(shù)據(jù)。本文將介紹ContentProvider的基礎(chǔ)應(yīng)用,感興趣的可以學(xué)習(xí)一下
    2021-12-12
  • Android GPS詳解及示例代碼

    Android GPS詳解及示例代碼

    本文主要講解Android GPS,這里整理了GPS 的基礎(chǔ)知識資料,并提供示例代碼和實現(xiàn)效果圖,有興趣的小伙伴可以參考下
    2016-08-08
  • android獲取時間差的方法

    android獲取時間差的方法

    這篇文章主要介紹了android獲取時間差的方法,涉及Android操作時間的相關(guān)技巧,需要的朋友可以參考下
    2015-04-04
  • ViewPager+Fragment實現(xiàn)側(cè)滑導(dǎo)航欄

    ViewPager+Fragment實現(xiàn)側(cè)滑導(dǎo)航欄

    這篇文章主要為大家詳細介紹了ViewPager+Fragment實現(xiàn)側(cè)滑導(dǎo)航欄,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-05-05
  • Android常用定時器的實現(xiàn)方式

    Android常用定時器的實現(xiàn)方式

    我們在開發(fā)中時常需要寫一些定時的任務(wù),比如每5秒執(zhí)行一次,下面這篇文章主要給大家介紹了關(guān)于Android常用定時器的實現(xiàn)方式,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-09-09
  • Android結(jié)束進程的方法詳解

    Android結(jié)束進程的方法詳解

    這篇文章主要介紹了Android結(jié)束進程的方法,結(jié)合實例形式分析了Android結(jié)束進程的具體步驟,實現(xiàn)技巧與相關(guān)注意事項,需要的朋友可以參考下
    2016-03-03
  • Android 應(yīng)用的歡迎界面實現(xiàn)代碼

    Android 應(yīng)用的歡迎界面實現(xiàn)代碼

    本文主要介紹Android 應(yīng)用歡迎界面的開發(fā),這里提供實現(xiàn)方法和實現(xiàn)代碼以供大家參考,有需要的朋友可以參考下
    2016-07-07
  • android 調(diào)用系統(tǒng)的照相機和圖庫實例詳解

    android 調(diào)用系統(tǒng)的照相機和圖庫實例詳解

    android手機有自帶的照相機和圖庫,我們做的項目中有時用到上傳圖片到服務(wù)器,今天做了一個項目用到這個功能,所以把我的代碼記錄下來和大家分享,有需求的朋友可以參考下
    2012-12-12

最新評論