单击时如何在文本视图中更改文本之前设置延迟?

时间:2016-05-22 20:36:47

标签: android

所以在我的应用程序中,我希望点击文本视图,文本更改,延迟3秒,然后将文本更改回原始文本。这是我到目前为止所做的。

        final TextView fact1 = (TextView) findViewById(R.id.fact1);
        fact1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            fact1.setText("Text");
            //Delay
            fact1.setText(Original text here, its stored in a text view with the id fact1);

        }
    });

我如何在文本视图切换之间延迟?

3 个答案:

答案 0 :(得分:4)

没有理由为此创建新的postDelayed。只需在TextView上使用fact1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { final String originalText = fact1.getText().toString(); fact1.setText("New Text"); fact1.postDelayed(new Runnable() { @Override public void run() { fact1.setText(originalText); } }, 3000); } });

/([+-]?\d*\.?\d*)%2c([+-]?\d*\.?\d*)/i

答案 1 :(得分:0)

您可以像这样使用CountDownTimer

String firstText=mTextField.getText().toString();
  new CountDownTimer(30000, 1000) {       
    public void onTick(long millisUntilFinished) { 

           mTextField.setText("seconds remaining: " +               millisUntilFinished / 1000); //here you can have your logic to set text to edittext

   } 

public void onFinish() { 
mTextField.setText(firstText); }

 }.start();

答案 2 :(得分:0)

你只需要在一个线程中运行代码并调用Thread.sleap

    Button button = (Button) findViewById(R.id.button);
    final TextView textView = (TextView) findViewById(R.id.textView2);
    final String[] originalText = {""};
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new Thread() {
                @Override
                public void run() {
                    originalText[0] = textView.getText().toString();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            textView.setText("what ever!!");
                        }
                    });
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            textView.setText(originalText[0]);
                        }
                    });
                }
            }.start();
        }
    });

通过使用lambda,代码将更加简洁

    Button button = (Button) findViewById(R.id.button);
    final TextView textView = (TextView) findViewById(R.id.textView2);
    final String[] originalText = {""};
        button.setOnClickListener(view -> {
        new Thread() {
            @Override
            public void run() {
                originalText[0] = textView.getText().toString();
                runOnUiThread(() -> textView.setText("what ever!!"));

                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                runOnUiThread(() -> textView.setText(originalText[0]));
            }
        }.start();
    });