如何在textview中设置递减计数器?

时间:2016-10-14 10:09:36

标签: android

目前,我正在开发模式锁定应用程序。在我的应用程序中,当用户输入错误的模式时,模式锁定活动延迟30秒。我在模式锁定活动上添加一个textview,只能显示“Please Wait”等文本30秒“。所以,我想将那个文本”请等待30秒“替换为递减计数器。

示例: 30-29-28-27 ----- 0(自动递减计数器值)

这是我的Textview xml

  <TextView
    android:id="@+id/txtMessage"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="Please Wait 30 Seconds"
    android:gravity="center"
    android:textSize="25dp"
    android:textColor="@color/colorAccent"
    android:visibility="gone"/>

请参阅我要设置递减计数器的android:text="Please Wait 30 Seconds"

这是我的活动代码

 private void invalidPattern() {

    failedCount ++;

    if (failedCount > LIMIT) {

        lock9View.setVisibility(View.GONE);
        txtMessage.setVisibility(View.VISIBLE);
        Animation zoomOutAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.animation);
        txtMessage.startAnimation(zoomOutAnimation);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                //reset the failed count
                failedCount = 0;
                lock9View.setVisibility(View.VISIBLE);
                txtMessage.setVisibility(View.GONE);
                //Enable the input interface here
            }
        }, 30000); // 30Sec delay
    }
    else
    {
        Log.i("Wrong....", "aa..else.....invalidPattern()");
    }
}

所以,如果有人知道这一点。

3 个答案:

答案 0 :(得分:2)

我认为您的要求的好方法是使用CountDownTimer。下面是代码,演示了如何使用

XML布局文件:

   <TextView
        android:id="@+id/txtMessage"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Please Wait 30 Seconds"
        android:gravity="center"
        android:textSize="25dp"
        android:textColor="@color/colorAccent"
        android:visibility="visible"/>
JAVA文件中的

    private TextView countDownTv;

    CountDownTimer mCountDownTimer = new CountDownTimer(30 * 1000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            //this will be called every second.
            countDownTv.setText("Please Wait " + (millisUntilFinished/1000) + " seconds.");
        }

        @Override
        public void onFinish() {
            //you are good to go.
            //30 seconds passed.
        }
    };

    @Override
    public void onCreate() {
        super.onCreate();
        //......
        countDownTv = (TextView) findViewById(R.id.txtMessage);

        //To start the timer...
        mCountDownTimer.start()
    }

答案 1 :(得分:0)

使用CountDownTimer

private class StoryCountDown extends CountDownTimer {

    public StoryCountDown(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
    }

    @Override
    public void onTick(long millisUntilFinished) {
        mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
    }

    @Override
    public void onFinish() {
        mTextField.setText("done!");
    }
}

并使用如下所示的课程。

StoryCountDown countDownTimer = new StoryCountDown(30000, 1000);
countDownTimer.start();

答案 2 :(得分:0)

除了其他答案:

Thread t = new Thread() {
        public void run() {             
            for (int i = 30; i > 0; i--) {                   
                try {
                    final int a = i;
                    runOnUiThread(new Runnable() {
                        public void run() {                        
                            txtMessage.setText("Please Wait "+ i +" seconds");                            
                        }                       
                    });
                    sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    };
    t.start();