2014-04-05 114 views
1

我想在幾秒鐘後用某個時間計數器自動打開我的充氣彈出窗口。我還不知道如何使用計時器(5秒)。一段時間後自動關閉窗口。怎麼辦

LayoutInflater inflater = (LayoutInflater)screen.getSystemService(screen.LAYOUT_INFLATER_SERVICE); 
        layout = inflater.inflate(R.layout.log_viewer,null); 

        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); 
        builder.setCancelable(true); 
        builder.setView(layout); 

        AlertDialog alertDialog = builder.create(); 
        alertDialog.show(); 

        Button btn0= (Button)layout.findViewById(R.id.btn0); 
        Button btn1= (Button)layout.findViewById(R.id.btn1); 
        Button btn2= (Button)layout.findViewById(R.id.btn2); 
        btn0.setOnClickListener(new OnClickListener() { 

         @Override 
         public void onClick(View v) { 

          Intent i =new Intent(Main_Activity.this,Act.class); 
          startActivity(i); 
          overridePendingTransition(R.anim.animation,R.anim.animation2); 
         } 
        }); 
        btn1.setOnClickListener(new OnClickListener() { 

         @Override 
         public void onClick(View v) { 

          Intent i =new Intent(Main_Activity.this,Activity2.class); 
          startActivity(i); 
          overridePendingTransition(R.anim.animation,R.anim.animation2); 
         } 
        }); 
        btn2.setOnClickListener(new OnClickListener() { 

         @Override 
         public void onClick(View v) { 

          Intent i =new Intent(Main_Activity.this,Activity1.class); 
          startActivity(i); 
          overridePendingTransition(R.anim.animation,R.anim.animation2); 
         } 
        }); 


        WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); 
        Window window = alertDialog.getWindow(); 
        lp.copyFrom(window.getAttributes()); 

        lp.width = WindowManager.LayoutParams.MATCH_PARENT; 
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT; 
        window.setAttributes(lp); 
+0

注意將來的讀取:OP不使用PopupWindow,而使用AlertDialog。 –

回答

3

您是否考慮過使用Toast進行自定義佈局?以Android 開發人員站點上的Toast documentation爲例。

否則如果你想延遲一定時間後要執行的操作,您可以使用一個Handler

Handler handler = new Handler(); 
handler.postDelayed(new Runnable() { 

    @Override 
    public void run() { 
     // close your dialog 
     alertDialog.dismiss(); 
    } 

}, 10000); // 10,000 ms delay 
0

,你可以做到以下幾點:

final long TOTAL_TIME = 5000; // miliseconds 

new Handler().postDelayed(new Runnable() { 
    public void run() { 
     // do action here 
    } 
}, TOTAL_TIME); 
2

延長自己PopupWindow窗口類像這

public class ToolTipWindow extends PopupWindow implements PopupWindow.OnDismissListener { 

    private long closeDelayTime; 
    private Handler mHandler; 
    private OnDismissListener mOnDismissListener; 

    public ToolTipWindow(Context context) { 
     super(context); 
     super.setOnDismissListener(this); 
    } 

    public ToolTipWindow(Context context, long closeDelayTime) { 
     this(context); 
     this.closeDelayTime = closeDelayTime; 
    } 

    @Override 
    public void setOnDismissListener(OnDismissListener onDismissListener) { 
     mOnDismissListener = onDismissListener; 
    } 

    //override all show or update method of super class that you use tho call onshow 
    @Override 
    public void showAtLocation(View parent, int gravity, int x, int y) { 
     onShow(); 
     super.showAtLocation(parent, gravity, x, y); 
    } 

    public void onShow() { 
     schedule(); 
    } 

    private void schedule() { 
     if (closeDelayTime > 0) { 
      if (mHandler == null) { 
       mHandler = new Handler(); 
      } 
      mHandler.removeCallbacks(closeRunnable); 
      mHandler.postDelayed(closeRunnable, closeDelayTime); 
     } 
    } 

    @Override 
    public void onDismiss() { 
     if (mHandler != null) { 
      mHandler.removeCallbacks(closeRunnable); 
     } 
     if (mOnDismissListener != null) { 
      mOnDismissListener.onDismiss(); 
     } 
    } 

private Runnable closeRunnable = new Runnable() { 
    @Override 
    public void run() { 
     dismiss(); 
    } 
}; 

} 

這裏我只重寫showAtLocation方法,如果您使用另一個PopUpWindow方法來顯示,重寫該方法並調用其中的onShow()來自動調度。

代碼中的某處我調用了mHandler.removeCallbacks(closeRunnable),調用它非常重要,因爲如果在時間流逝之前關閉了widow,它就會避免崩潰。

+0

加入closeRaunnable –

相關問題