1

我有一个工作正常的弹出按钮。当我点击它时,它会被解雇。但是如果用户不对其采取任何操作,我还想添加一个代码以在 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

4 回答 4

1

得到一个像这样的 CountDownTimer,

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

私人无效getPopUpDismissTimer(长millisInFuture,长countDownInterval){PopUpDismissTimer =新CountDownTimer(millisInFuture,countDownInterval){

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

    @Override
    public void onTick(long millisUntilFinished) {

    }
};

}

现在,在您想要关闭弹出窗口的地方调用此倒数计时器,例如 -

getPopUpDismissTimer(5000, 1000); //5000 毫秒是你想要关闭弹窗的时间 mPopUpDismissTimer.start();

于 2012-10-29T06:28:10.413 回答
0

尝试启动新线程,让这个线程休眠 5 秒,然后关闭对话框

就像是:

新线程(){运行(){线程.sleep(5000);在此处关闭对话框 } }.start();

于 2012-10-29T06:20:59.767 回答
0

使用处理程序

功能postDelayed(Runnable r, long delayMillis)还是sendMessageDelayed(Message msg, long delayMillis)可以使用的。

于 2012-10-29T06:21:03.193 回答
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
        }
    }});

要隐藏弹出窗口,您必须实现一个处理程序来处理 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;
        }
    }
};
于 2012-10-29T06:27:10.853 回答