最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

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

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

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

本文使用方法三進(jìn)行實(shí)現(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;
 
/**
 * 自定義時(shí)長(zhǎng)的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;
  }
 }
}

測(cè)試類的代碼如下:

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(), "我來(lái)自CToast!",time);
  mCToast.show();
  break;
 
 case R.id.hideToastBtn:
  if(null!=mCToast)
  {
  mCToast.hide();
  }
  break;
 }
 
 }
 };
 
}

效果如下:

源碼下載:android自定義Toast設(shè)定顯示時(shí)間

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

相關(guān)文章

  • Android實(shí)現(xiàn)視頻圖片輪播功能

    Android實(shí)現(xiàn)視頻圖片輪播功能

    視頻與圖片輪播功能是一種常見(jiàn)的用戶交互方式,廣泛應(yīng)用于多媒體展示、廣告輪播、資訊閱讀、產(chǎn)品推薦等場(chǎng)景,在這種模式下,應(yīng)用能同時(shí)支持圖片和視頻的自動(dòng)或手動(dòng)輪播播放,本項(xiàng)目旨在基于 Android 平臺(tái),構(gòu)建一個(gè)支持視頻與圖片混合輪播的解決方案,需要的朋友可以參考下
    2025-04-04
  • Android 開(kāi)發(fā)隨手筆記之使用攝像頭拍照

    Android 開(kāi)發(fā)隨手筆記之使用攝像頭拍照

    在Android中,使用攝像頭拍照一般有兩種方法, 一種是調(diào)用系統(tǒng)自帶的Camera,另一種是自己寫(xiě)一個(gè)攝像的界面,本篇文章給大家介紹android開(kāi)發(fā)隨手筆記之使用攝像頭拍照,感興趣的朋友一起學(xué)習(xí)吧
    2015-11-11
  • Android實(shí)現(xiàn)短信加密功能(發(fā)送加密短信、解密本地短信)

    Android實(shí)現(xiàn)短信加密功能(發(fā)送加密短信、解密本地短信)

    這篇文章主要介紹了android實(shí)現(xiàn)短信加密功能的相關(guān)資料,功能包括發(fā)送加密短信、解密本地短信,感興趣的小伙伴們可以參考一下
    2016-01-01
  • android FM播放時(shí)拔出耳機(jī)后FM APP自動(dòng)close解決方法

    android FM播放時(shí)拔出耳機(jī)后FM APP自動(dòng)close解決方法

    android FM播放時(shí)拔出耳機(jī)后FM APP自動(dòng)close關(guān)閉的情況應(yīng)該怎樣解決呢?下面為大家詳細(xì)介紹下具體修改方法,感興趣的朋友可以參考下
    2013-06-06
  • Android中使用BitmapShader類來(lái)制作各種圖片的圓角

    Android中使用BitmapShader類來(lái)制作各種圖片的圓角

    這篇文章主要介紹了Android中使用BitmapShader類來(lái)制作各種圖片的圓角的方法,文中隨教程講解帶出的例子可以輕松控制圖片圓形的變換,很好很強(qiáng)大,需要的朋友可以參考下
    2016-04-04
  • Android retrofit上傳文件實(shí)例(包含頭像)

    Android retrofit上傳文件實(shí)例(包含頭像)

    下面小編就為大家分享一篇Android retrofit上傳文件實(shí)例(包含頭像),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-01-01
  • Android Studio4.0解決Gradle下載超時(shí)問(wèn)題

    Android Studio4.0解決Gradle下載超時(shí)問(wèn)題

    這篇文章主要介紹了Android Studio4.0解決Gradle下載超時(shí)問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • 詳解Android中IntentService的使用方法

    詳解Android中IntentService的使用方法

    這篇文章主要介紹了Android中IntentService的使用方法,需要的朋友可以參考下
    2015-12-12
  • Android自定義ProgressDialog進(jìn)度等待框

    Android自定義ProgressDialog進(jìn)度等待框

    這篇文章主要介紹了Android自定義ProgressDialog進(jìn)度等待框,通過(guò)本文大家可以嘗試?yán)肁ndroid自定義ProgressDialog,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-01-01
  • Android Studio實(shí)現(xiàn)進(jìn)度條效果

    Android Studio實(shí)現(xiàn)進(jìn)度條效果

    這篇文章主要為大家詳細(xì)介紹了Android Studio實(shí)現(xiàn)進(jìn)度條效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-04-04

最新評(píng)論

葫芦岛市| 扶绥县| 特克斯县| 上林县| 合作市| 平顺县| 辉南县| 沐川县| 扶沟县| 福建省| 莱阳市| 乌拉特前旗| 梅河口市| 长治县| 中山市| 伽师县| 贵南县| 宁陕县| 深州市| 武陟县| 京山县| 朝阳市| 芦山县| 汽车| 清河县| 固镇县| 陇川县| 沭阳县| 陈巴尔虎旗| 莱芜市| 通江县| 琼中| 徐州市| 海丰县| 石家庄市| 盐边县| 福海县| 绵竹市| 东辽县| 文登市| 南澳县|