首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android编辑系统文件

Android编辑系统文件
EN

Stack Overflow用户
提问于 2014-10-09 21:21:32
回答 1查看 143关注 0票数 0

我最近开始为android开发我的第一个应用程序,我有点卡住了。我的应用程序使用了CountDownTimer类,似乎malfuncions - CountDownTimer.cancel()不起作用。我知道如何通过修改CountDownTimer.java文件来修复这个问题,但我没有这样做的权限。我在Android Studio工作。我在网上找不到一个解释,尽管这似乎是一个很基本的问题,不是吗?也许我用的词不对?

非常感谢你的帮助,如果我的问题有点无知,我道歉哈哈。

EN

回答 1

Stack Overflow用户

发布于 2014-10-09 21:25:18

尝试这样做:

代码语言:javascript
复制
public class Main extends Activity implements OnClickListener
    {
        private static final String tag = "Main";
        private MalibuCountDownTimer countDownTimer;
        private long timeElapsed;
        private boolean timerHasStarted = false;
        private Button startB;
        private TextView text;
        private TextView timeElapsedView;

        private final long startTime = 50000;
        private final long interval = 1000;

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState)
            {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                startB = (Button) this.findViewById(R.id.button);
                startB.setOnClickListener(this);

                text = (TextView) this.findViewById(R.id.timer);
                timeElapsedView = (TextView) this.findViewById(R.id.timeElapsed);
                countDownTimer = new MalibuCountDownTimer(startTime, interval);
                text.setText(text.getText() + String.valueOf(startTime));
            }

        @Override
        public void onClick(View v)
            {
                if (!timerHasStarted)
                    {
                        countDownTimer.start();
                        timerHasStarted = true;
                        startB.setText("Start");
                    }
                else
                    {

                        countDownTimer.cancel();
                        timerHasStarted = false;
                        startB.setText("RESET");
                    }
            }

        // CountDownTimer class
        public class MalibuCountDownTimer extends CountDownTimer
            {

                public MalibuCountDownTimer(long startTime, long interval)
                    {
                        super(startTime, interval);
                    }

                @Override
                public void onFinish()
                    {
                        text.setText("Time's up!");
                        timeElapsedView.setText("Time Elapsed: " + String.valueOf(startTime));
                    }

                @Override
                public void onTick(long millisUntilFinished)
                    {
                        text.setText("Time remain:" + millisUntilFinished);
                        timeElapsed = startTime - millisUntilFinished;
                        timeElapsedView.setText("Time Elapsed: " + String.valueOf(timeElapsed));
                    }
            }
    }

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26279526

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档