2017-04-25 69 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