[Android]自定義定時(shí)消失PopupWindow

使用CountDownTimer類(lèi)監(jiān)聽(tīng)PopupWindow, 實(shí)現(xiàn)提示彈窗消息,讓用戶不用點(diǎn)擊過(guò)段時(shí)間自動(dòng)消失,優(yōu)化用戶體驗(yàn)。

public class PopupView extends PopupWindow {    
    private View mPopupView;    
    private TextView mTextView;    
    private CountTimer countTimer;  
                                                         
    public PopupView(Activity context, String text, int keep_alive) {
        super(context);      
        LayoutInflater.inflater(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);        
        mPopupView = inflater.inflate(R.layout.popupwindow_message, null);   
        mTextView = (TextView)mPopupView.findViewById(R.id.popupwindow_messgae_text);        
        mTextView.setText(text);  
        this.setContentView(mPopupView);  
        this.setWidth(LayoutParams.FILL_PARENT);    
        this.setHeight(LayoutParams.WRAP_CONTENT);     
        this.setFocusable(true);       
        this.setBackgroundDrawable(new ColorDrawable(0xb0000000));        
        countTimer = new CountTimer(keep_alive*1000 + 1000, 500);        
        countTimer.start();    
        mPopupView.setOnTouchListener(new OnTouchListener() {            
            public boolean onTouch(View v, MotionEvent event) {                
                int height = mPopupView.findViewById(R.id.popupwindow_messgae_id).getTop();                
                int y=(int) event.getY();    
                if(event.getAction()== MotionEvent.ACTION_UP){   
                    if(y<height){                        
                        dismiss();                        
                        countTimer.cancel();                   
                    }                
                }                
                return true;            
            }        
        });    
     }    
     public void setText(String text){    
         mTextView.setText(text);    
     }    
     public void cancelTimer(){        
         countTimer.cancel();    
     }    
     private class CountTimer extends CountDownTimer { 
         public CountTimer(long millisInFuture, long countDownInterval) {
             super(millisInFuture, countDownInterval);        
         }        
         @Override        
         public void onFinish() {
             cancel();        
         }        
         @Override        
         public void onTick(long millisUntilFinished) {  
             if (millisUntilFinished < 1000 && PopupView.this.isShowing()) { 
                 PopupView.this.dismiss();            
             }        
         }    
     }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,368評(píng)論 25 708
  • ¥開(kāi)啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開(kāi)一個(gè)線程,因...
    小菜c閱讀 6,524評(píng)論 0 17
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,245評(píng)論 4 61
  • 轉(zhuǎn)載本文需注明出處:微信公眾號(hào)EAWorld,違者必究。 本文目錄: 一、寫(xiě)在前面 二、AIDevOps,未來(lái)已來(lái)...
    72a1f772fe47閱讀 239評(píng)論 0 1
  • yanhsJ閱讀 149評(píng)論 0 1