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

            }
            };
            }});

最佳答案

获得一个这样的CountDownTimer,

私有CountDownTimer mPopUpDismissTimer; //实例变量,放在您的活动类中


  私人无效getPopUpDismissTimer(long millisInFuture,long 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 ms是您要关闭弹出窗口的时间
    mPopUpDismissTimer.start();

10-04 11:49
查看更多