我怎么能留下柜台代码?

时间:2016-08-29 10:27:31

标签: java android counter

我的问题: 我不知道,我怎么能留下我的计数器代码(CountDownTimer),如果用户点击计数器内的imageButton 2秒

我的代码:

final ImageButton avatarScroll = (ImageButton) findViewById(R.id.imgBtnAvatarback);

avatarScroll.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ursprungAvatar = !ursprungAvatar;
            if(ursprungAvatar) {
                //image change with arrow
                ImageButton avatarScroll = (ImageButton) findViewById(R.id.imgBtnAvatarback);
                avatarScroll.setImageResource(R.drawable.avatarback_pfeil);
                new CountDownTimer(2000, 1000) {
                    public void onTick(long millisUntilFinished) {
                        //
                    }
                    public void onFinish() {
                        //image change without arrow
                        ImageButton avatarScroll = (ImageButton) findViewById(R.id.imgBtnAvatarback);
                        avatarScroll.setImageResource(R.drawable.avatarback);
                        ursprungAvatar = !ursprungAvatar;
                    }
                }.start();

            } else {
                //image change without arrow
                ImageButton avatarScroll_ursprung = (ImageButton) findViewById(R.id.imgBtnAvatarback);
                avatarScroll_ursprung.setImageResource(R.drawable.avatarback);
            }

    }
});

1 个答案:

答案 0 :(得分:1)

如果我的行为正确,您需要做的就是在else案例中添加timer.cancel()并保留对创建的计时器的引用(例如,将其设为字段)。