Android:弹出按钮按时解散?

时间:2012-10-29 06:12:27

标签: android

我有一个正常工作的弹出按钮。当我点击它时它会被解雇。 但是如果用户不采取任何措施,我还想在5秒后添加代码来解除弹出窗口?这可能吗?

当前代码

final ImageButton rredButton=(ImageButton)findViewById(R.id.RredButton);
    rredButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {
            LayoutInflater layoutInflater
            = (LayoutInflater)getBaseContext()      
            .getSystemService(LAYOUT_INFLATER_SERVICE);
            View popupView = layoutInflater.inflate(R.layout.popupright, null);
            final PopupWindow popupWindow = new PopupWindow(               
                    popupView,                
                    LayoutParams.WRAP_CONTENT,                       
                    LayoutParams.WRAP_CONTENT);     
            Button btnNxtScr = (Button)popupView.findViewById(R.id.nextscreen);             
            btnNxtScr.setOnClickListener(new Button.OnClickListener(){     
                @Override     
                public void onClick(View v) {      
                    Intent myintent1 = new Intent(colorActivity.this,colorBlueActivity.class);
                    startActivity(myintent1);
                }
            });
                    popupWindow.showAtLocation(rredButton, Gravity.CENTER, 0, 0);
                    //---                          
                    popupWindow.setFocusable(true);             
                    popupWindow.update();                          
                    //---
        }});

这是我更新的代码。有什么问题?

final ImageButton rredButton=(ImageButton)findViewById(R.id.RredButton);
    rredButton.setOnClickListener(new View.OnClickListener() {

        private CountDownTimer mPopUpDismissTimer; 
        public void onClick(View arg0) {
            LayoutInflater layoutInflater
            = (LayoutInflater)getBaseContext()      
            .getSystemService(LAYOUT_INFLATER_SERVICE);
            View popupView = layoutInflater.inflate(R.layout.popupright, null);
            final PopupWindow popupWindow = new PopupWindow(               
                    popupView,                
                    LayoutParams.WRAP_CONTENT,                       
                    LayoutParams.WRAP_CONTENT);     

            getPopUpDismissTimer(3000, 1000);
            mPopUpDismissTimer.start(); 

        }
            private void getPopUpDismissTimer(long millisInFuture, long countDownInterval) { 
            mPopUpDismissTimer = new CountDownTimer(millisInFuture, countDownInterval) {


            @Override
            public void onFinish() {
                Button btnNxtScr = (Button)popupView.findViewById(R.id.nextscreen);             
                btnNxtScr.setOnClickListener(new Button.OnClickListener(){     

                @Override     
                public void onClick(View v) {      
                    Intent myintent1 = new Intent(colorActivity.this,colorBlueActivity.class);
                    myintent1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(myintent1);

                                };

        });
                popupWindow.showAtLocation(rredButton, Gravity.CENTER, 0, 0);
                //---                          
                popupWindow.setFocusable(true);             
                popupWindow.update();                          
                //---
            }

            @Override
            public void onTick(long millisUntilFinished) {

            }
            };
            }});

4 个答案:

答案 0 :(得分:1)

像这样获取CountDownTimer,

private CountDownTimer mPopUpDismissTimer; //实例变量,放入你的活动类

  

private void getPopUpDismissTimer(long millisInFuture,long countDownInterval){       PopUpDismissTimer = new CountDownTimer(millisInFuture,countDownInterval){

    @Override
    public void onFinish() {
      // put your logic for dismissing popup button
    }

    @Override
    public void onTick(long millisUntilFinished) {

    }
};

}

现在,调用此倒计时器,您要在其中关闭弹出窗口,例如 -

  

getPopUpDismissTimer(5000,1000); // 5000 ms是您要取消弹出窗口的时间     mPopUpDismissTimer.start();

答案 1 :(得分:0)

尝试启动新线程,将此线程休眠5秒钟,然后点击dismissdialog

类似的东西:

new Thread(){     跑(){     了Thread.sleep(5000);     在这里解雇对话     } }。开始();

答案 2 :(得分:0)

使用Handler

可以使用函数postDelayed(Runnable r, long delayMillis)sendMessageDelayed(Message msg, long delayMillis)

答案 3 :(得分:0)

在您的课程中添加以下行:

private static final int MSG_AUTO_DISMISS = 0;

然后在你的onClick代码中:

final ImageButton rredButton=(ImageButton)findViewById(R.id.RredButton);
    rredButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {
            //call this when displaying the dialog
            handler.sendEmptyMessageDelayed(MSG_AUTO_DISMISS, 5000);

            //your own code here
        }
    }});

隐藏弹出窗口,你必须实现一个Handler来处理MSG_AUTO_DISMISS消息,所以将以下代码添加到你的类中:

private Handler handler = new Handler() {
    @SuppressWarnings("deprecation")
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
        case MSG_AUTO_DISMISS:
                            //dismiss your popup here

                            handler.removeMessages(MSG_AUTO_DISMISS); //add this just in case
            break;
        }
    }
};