丶紫气东来 2021-01-19 09:32 采纳率: 20%
浏览 37
已采纳

怎么改变文本,数秒后又恢复原来的文本?

一个textView,显示的文本内容是a;在一个事件后,文本内容变为b,且保持数秒后,恢复为a。怎么实现这个效果?

我已经尝试过的方法:1.在一个自定义的handler类a的run方法中,将文本a改为文本b;为了文本b保持数秒的效果,我又自定义了一个handler类b,在handler类b的run方法中使用thread.sleep(数秒)方法。在把文本a改为文本b后,用handler类b的对象发送消息,将文本b保持数秒,然后恢复为文本a。

  • 写回答

5条回答 默认 最新

  • booooo­ 2021-01-19 11:50
    关注

    。。。。。

    兄弟,得学会自己想办法呀。

    算了,给你一个完整的Demo吧。

    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.widget.Button;
    import android.widget.TextView;
    
    import androidx.annotation.NonNull;
    import androidx.annotation.Nullable;
    import androidx.appcompat.app.AppCompatActivity;
    
    import java.lang.ref.SoftReference;
    import java.text.SimpleDateFormat;
    import java.util.Locale;
    import java.util.Timer;
    import java.util.TimerTask;
    
    public class HomeActivity extends AppCompatActivity {
    
        private TextView textView; // TextView对象
    
        private boolean isNeedKeep = false; // 开关(表示是否开启保持)
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            textView = new TextView(this); // TextView对象
    
            this.elapsedTime(); // 启动计时器,并追随当前页面的生命周期
    
            new Button(this).setOnClickListener(v -> { // 模拟一个事件
                textView.setText("b"); // 触发事件后TextView的内容变为:b
                isNeedKeep = true;
            });
        }
    
        private final Timer timer = new Timer();
        private final TimerHandler handler = new TimerHandler(this);
    
        private void elapsedTime() {
            timer.scheduleAtFixedRate(new TimerTask() {
                @Override
                public void run() {
                    handler.sendEmptyMessage(0);
                }
            }, 0, 1000);
        }
    
        private final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
    
        private String formaterTime(long timemillis) {
            if (timemillis <= 0) return dateFormat.format(System.currentTimeMillis());
            return dateFormat.format(timemillis);
        }
    
        private int keepSecond = 5; // b需要保持的时间(秒)
    
        private void setCurrentTime(long timemillis) {
            if (isNeedKeep && keepSecond > 0) {
                keepSecond--;
                return;
            }
            isNeedKeep = false; // 开关(关闭保持状态)
            keepSecond = 5; // 恢复需要保持的时间
            textView.setText(this.formaterTime(timemillis)); // 初始内容为:当前时间
        }
    
        private static class TimerHandler extends Handler {
            private final SoftReference<HomeActivity> reference;
    
            TimerHandler(HomeActivity activity) {
                reference = new SoftReference<>(activity);
            }
    
            private long timemillis = System.currentTimeMillis();
    
            @Override
            public void handleMessage(@NonNull Message msg) {
                HomeActivity activity = reference.get();
                if (null == activity) return;
                timemillis += 1000; // 每次时间都+1s
                activity.setCurrentTime(timemillis);
            }
        }
    
        @Override
        protected void onPause() {
            super.onPause();
            timer.cancel(); // 页面不处于栈顶时,计时器停止
        }
    }
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog