2017-04-25 71 views
-1

我在活動中有彈出窗口。我想要的是這個彈出窗口在3秒後開始,當活動被創建並且持續3秒鐘時。請幫忙嗎?活動創建3秒後顯示PopupWindow 3秒

這裏是我的代碼:

try { 
    LayoutInflater inflater1 = (LayoutInflater) MainActivity.this 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    // Inflate the view from a predefined XML layout 
    View layout = inflater1.inflate(R.layout.activity_pop_up, 
      (ViewGroup) findViewById(R.id.relativeLayoutZaFragment)); 
    // create a 300px width and 470px height PopupWindow 
    pw = new PopupWindow(layout, 300, 470, true); 
    // display the popup in the center 
    pw.showAtLocation(layout, Gravity.CENTER, 0, 0); 
} catch (Exception e) { 
    e.printStackTrace(); 
} 

回答

1

試試這個

boolean isShowing=false; 

在的onCreate

CountDownTimer timer=new CountDownTimer(3000,1000) { 
     @Override 
     public void onTick(long l) { 

     } 

     @Override 
     public void onFinish() { 
      if(isShowing){ 
       //CLOSE 
      } 
      else{ 
      isShowing=true; 
      LayoutInflater inflater1 = (LayoutInflater) 
        MainActivity.this 
          .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      //Inflate the view from a predefined XML layout 
      View layout = inflater1.inflate(R.layout.activity_pop_up, 
        (ViewGroup) findViewById(R.id.relativeLayoutZaFragment)); 
      // create a 300px width and 470px height PopupWindow 
      pw = new PopupWindow(layout, 300, 470, true); 
      // display the popup in the center 
      pw.showAtLocation(layout, Gravity.CENTER, 0, 0) 
      timer.start(); 
      } 
     } 
    }; 
    timer.start(); 
+0

我不能在oncreate()方法中實現這一點。 – smiljka

+0

布爾顯示上面創建,其餘在創建 –

+0

當我實現這個彈出不斷屏幕上,我需要以某種方式解僱 – smiljka

0

,其可與Handler發佈事件可以輕鬆實現。

private final Handler handler = new Handler(Looper.getMainLooper()); 
private PopupWindow popupWindow; 

private final Runnable dismissPopupRunnable = new Runnable() { 
    @Override 
    public void run() { 
     // dismiss popupWindow 
    } 
}; 

private final Runnable showPopupRunnable = new Runnable() { 
    @Override 
    public void run() { 
     // show popupWindow 
     handler.postDelayed(dismissPopupRunnable, 3000); 
    } 
}; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    // initialize popupWindow here 

    handler.postDelayed(showPopupRunnable, 3000); 
} 

@Override 
protected void onPause() { 
    super.onPause(); 
    handler.removeCallbacks(showPopupRunnable); 
    handler.removeCallbacks(dismissPopupRunnable); 
} 

注意,你必須採取從處理移除回調,活動被暫停時的照顧。

+0

這個不工作,彈出窗口不斷運行 – smiljka

+0

你說'popup window running running'是什麼意思? – azizbekian

+0

我並沒有放棄所有的時間..它在那裏所有的時間 – smiljka