首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用天文计时-在setText之后,文本视图仅显示文本一秒钟

使用天文计时-在setText之后,文本视图仅显示文本一秒钟
EN

Stack Overflow用户
提问于 2019-04-04 06:48:58
回答 1查看 43关注 0票数 0

我正在创建一个时间跟踪应用程序,并希望有一个秒表,可以启动/暂停/重置。当我按下“结束一天并保存”按钮时,我希望应用程序在下面的TevtView中显示经过的小时/分钟/秒。是的,但只有一秒钟,然后它就消失了。我哪里错了?

我尝试创建一个仅用于更新TextView的新方法,但它完全没有显示出来。如果有帮助,我必须在这个项目中使用的最小SDK版本是23。

代码语言:javascript
运行
复制
public void stopAndSaveChronometer(View v) {
    if (running) {
        chronometer.stop();
        running = false;
        time = SystemClock.elapsedRealtime() - chronometer.getBase();
        h = (int) (time / 3600000);
        m = (int) (time - h * 3600000) / 60000;
        s = (int) (time - h * 3600000 - m * 60000) / 1000;

        Intent secondsIntent = new Intent(this,
                trackTodayActivity.class).putExtra("saved seconds", s);

        startActivity(secondsIntent);

        Intent minsIntent = new Intent(this,
                trackTodayActivity.class).putExtra("saved mins", m);

        startActivity(minsIntent);

        Intent hoursIntent = new Intent(this,
                trackTodayActivity.class).putExtra("saved hours", h);

        startActivity(hoursIntent);

        Bundle extras = getIntent().getExtras();

       if (extras != null) {
            seconds = extras.getInt("saved seconds", s);
            minutes = extras.getInt("saved minutes", m);
            hours = extras.getInt("saved hours", h);

        }

        // set up Strings to hold the times (already initialised as global variables)

        printseconds = Integer.toString(s);
        printminutes = Integer.toString(m);
        printhours = Integer.toString(h);

        // set up String to display in TextView (already initialised as global variable)

        displayTheTime = (printhours + " hours, ")
                + (printminutes + " minutes, ")
                + (printseconds + " seconds");

        // display the times in the Textview

        showMyTime = (TextView) findViewById(R.id.printsavedTime);
        showMyTime.setText(displayTheTime);

    }

}

}
EN

Stack Overflow用户

回答已采纳

发布于 2019-04-04 18:53:20

为什么你要发布3次trackTodayActivity?这个活动有用吗?

要显示结果,这应该足够了:

代码语言:javascript
运行
复制
public void stopAndSaveChronometer(View v) {
  if (running) {
    chronometer.stop();
    running = false;
    time = SystemClock.elapsedRealtime() - chronometer.getBase();
    h = (int) (time / 3600000);
    m = (int) (time - h * 3600000) / 60000;
    s = (int) (time - h * 3600000 - m * 60000) / 1000;

    displayTheTime = String.valueOf(h) + " hours, "
            + String.valueOf(m) + " minutes, "
            + String.valueOf(s) + " seconds";

    // display the times in the Textview
    showMyTime.setText(displayTheTime);

  }
}

这可以在您的活动的onCreate方法中完成:

代码语言:javascript
运行
复制
showMyTime = (TextView) findViewById(R.id.printsavedTime);
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55505451

复制
相关文章

相似问题

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