想要延迟 1 秒显示数字

2024-01-29

我想在 TextView 中显示数字,如下所示:

Wait 5 sec // then a delay of 1 sec
Wait 4 sec // display this in the same text view along with delay
Wait 3 sec // display this in the same text view along with delay
Wait 2 sec // display this in the same text view along with delay
Wait 1 sec // display this in the same text view along with delay

我想在单击按钮时执行此操作,它应该像倒计时器一样工作。


非常简单,您需要使用倒计时器 http://developer.android.com/reference/android/os/CountDownTimer.html class.

这是我刚刚为您创建的一个工作演示。

MainActivity.java

public class MainActivity extends Activity 
{
    int remaningTime = 5;
    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById( R.id.txtTextView );
        Button button1 = (Button) findViewById( R.id.button1 );

        button1.setOnClickListener( new OnClickListener ()
        {

            @Override
            public void onClick(View arg0) 
            {
                new CountDownTimer ( remaningTime * 1000, 1000 )
                {

                    @Override
                    public void onFinish() 
                    {
                    }

                    @Override
                    public void onTick(long millisUntilFinished) 
                    {
                        textView.setText( "Wait " + ( remaningTime-- ) + " sec" );
                    }
                }.start();
            }           
        });
    }
}

主文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/txtTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/txtTextView"
        android:layout_below="@+id/txtTextView"
        android:layout_marginTop="34dp"
        android:text="Button" />

</RelativeLayout>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

想要延迟 1 秒显示数字 的相关文章

随机推荐

  • 有没有办法使导航栏徽标响应?

    这是我使用 bootstrap 4 构建的网站 我对徽标有一个大问题 因为它在所有设备上保持相同的大小 我尝试过添加 img fluid 但是如果添加这个类 徽标在手机上会缩小很多 看起来就像一个小点 所以我删除了这个类 现在 在移动设备上
  • 通用 Promise 重试逻辑

    我试图弄清楚如何创建一个通用重试函数 该函数对于传递给它的任何承诺都会呈指数级回退 看起来除了几件事之外一切都正常 如果该函数在第一次尝试时解析 那么我会看到我的解析值 并且它会输出嘿 这是预期的 如果它在任何后续调用中解决 它不会注销嘿
  • 使用 yajl-objc 编码自定义类

    Summary 基于一些基准 http samsoff es posts updated iphone json benchmarks 我选择了yajl objc http github com gabriel yajl objc用于我的
  • android.view.WindowManager$BadTokenException 异常。有什么指针吗?

    我有两个 webview 为了从两个 webview 中删除通用功能 我创建了一个超类 其中包含超类中的所有方法 并在两个 webview 中使用它 有一次 当我创建对象并设置第一个 Web 视图中的变量时 Web 视图正确显示 当我按 返
  • Javascript 闭包/变量作用域问题 - 我知道它有效,但为什么?

    我用 JS 开发已经有一段时间了 虽然我知道下面的代码可以工作 但我不太明白why有用 在我看来 我在 testClosure 函数中定义了 testString 并且我期望该变量在 testClosure 函数完成时 消失 因为它是局部变
  • 创建属性设置器委托

    我创建了将属性 lambda 转换为委托的方法 public static Delegate MakeGetter
  • 在 C# 中通过 AWS.NET 从 S3 存储检索二进制数据

    我已经测试了适用于 NET 的 AWS 开发工具包中包含的大部分示例 它们都运行良好 我可以将对象 列表对象和删除对象放入存储桶中 但是 假设我删除了原始对象并想要同步本地丢失的那些文件 我想创建一个 GET 对象 按键 名称和原因桶 我可
  • 如何为Lua表添加前缀?

    我有一个lua文件 其内容为lua表如下 A A B A B C 0 问题是我想在上述每个语句之前添加前缀 XYZ 因此 解析之后数据库应该有这样的内容 XYZ A XYZ A B XYZ A B C 有任何想法吗 提前致谢 您可以使用以下
  • 熊猫表查找

    我有一个熊猫查找表 看起来像这样 Grade Lower Boundary Upper Boundary 1 110 96 2 96 91 3 91 85 4 85 81 5 81 77 6 77 72 7 72 68 8 68 63 9
  • SVG 和 Javascript - 创建 SVGPoint - TypeError:非法构造函数

    我正在尝试使用 Javascript 创建 SVG 多边形 当我尝试使用以下 Javascript 代码创建 SVGPoint 时 var p new SVGPoint 我收到以下消息 类型错误 非法构造函数 您需要从您的 SVG 文档中调
  • Espresso:如何滚动到 ScrollView 的底部

    在 Espresso 测试中如何向下滚动到 ScrollView 的底部 谢谢 如果在 ScrollView 的底部您需要找到一个视图并与之匹配某些内容 那么只需执行scrollTo 在需要显示它的任何其他操作之前对其执行操作 onView
  • C# 中的 Windows 时区下拉菜单 [关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 是否有一种简单的方法可以在 C 应用程序中显示 Windows 时区下拉列表 以便用户可以更改它 类似于这个时区下拉菜单 https
  • 不要从 UITableView 中删除某些行

    我正在尝试实现从 a 中删除一些行的功能table view而不是其他人 在这种情况下 一切都在section 0不应该被删除 所以也不能滑动删除 但是里面的所有内容section 1应该可以 我怎样才能实现这个 现在section 0ro
  • Ui 表单:添加运行时还是编译时?

    如果我有大量 UI 表单 我想知道哪种方法有效 我对这两种情况下的内存利用率没有任何想法 我只是在一个简单的示例中尝试了两种方法 http qt project org doc qt 4 8 designer using a ui file
  • 检查 C 枚举中是否定义了某个值?

    假设我有这个 enum A 0x2E B 0x23 C 0x40 可以检查是否x被定义为enum 我正在手动执行此操作 int isdef x A x B x C 但我想要更有活力的东西 GCC extensions也欢迎 据我所知并非如此
  • 你可以在CSS文档中使用handlebars.js变量吗?

    我有兴趣做一些条件格式并认为最好的方法是做类似的事情 InsideCSSdocument if something some css else some other css if 但我搜索了一下 只发现Django相关的东西 这在车把或任何
  • 下载S3并添加到应用程序目录

    我有一个正在尝试设置的弹性 beanstalk Node 应用程序 我正在尝试将环境文件从 S3 加载到 var app ondeck src 因此该文件在 Node 应用程序启动时可用 我已经尝试了尽可能多的方法 但 production
  • 什么是双变量参数? ~ 打字稿

    TypeScript 的文档附带编译器选项 https www typescriptlang org docs handbook compiler options html 其中 strictFunctionTypes是用它的描述来定义的
  • Python tkFileDialog.asksaveasfile - 获取文件路径

    我想获取文件 exportFile 的路径 exportFile tkFileDialog asksaveasfile mode a 如果我写 打印导出文件 我会得到
  • 想要延迟 1 秒显示数字

    我想在 TextView 中显示数字 如下所示 Wait 5 sec then a delay of 1 sec Wait 4 sec display this in the same text view along with delay