国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

android自定義Toast設定顯示時間

2019-10-21 21:39:58
字體:
供稿:網(wǎng)友

開發(fā)android的同學可能會抱怨Toast設定顯示的時長無效,只能是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,顯示時間

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關教程知識閱讀請移步到Android開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 凭祥市| 军事| 丘北县| 东莞市| 额济纳旗| 黔东| 贵州省| 探索| 梨树县| 隆子县| 宁城县| 淮北市| 宣威市| 贵阳市| 威海市| 绥宁县| 华安县| 藁城市| 巴中市| 岐山县| 东港市| 黑龙江省| 麻栗坡县| 平昌县| 阜康市| 顺昌县| 东山县| 调兵山市| 浙江省| 威海市| 富阳市| 平陆县| 广丰县| 石城县| 厦门市| 沙河市| 宁夏| 孙吴县| 波密县| 赣榆县| 沙坪坝区|