如何在Android中设置文本之间设置延迟

时间:2018-07-25 15:54:19

标签: java android delay wait seconds

如何设置bt.setText(“№”)之间的多个延迟?

    public void buttonOnClick (View button) {
    final Button bt = findViewById(R.id.button);
    bt.setText("3");
    //wait 1 second
    bt.setText("2");
    //wait 1 second
    bt.setText("1");
    //wait 1 second
    bt.setText("Click!");

5 个答案:

答案 0 :(得分:2)

由于您无法在UI线程上调用Thread.sleep(只会显示最终结果),因此应在另一个线程上执行此操作,例如:

关于构造函数:

private Handler handler;
public void onCreate(Bundle x) {
     //super and get bt
     final Button bt = findViewById(R.id.button);
     handler = new Handler() {
           public void handleMessage(Message msg) {
                if(msg.what == 0)
                     bt.setText("Click!");
                else
                     bt.setText(String.toString(msg.what));
           }
     }
}

public void buttonOnClick (View button) {
   final Button bt = findViewById(R.id.button);
   bt.setText("3");
   //wait 1 second
   handler.sendEmptyMessageDelayed(2, 1000);
   //wait 2 second
   handler.sendEmptyMessageDelayed(1, 2000);
   //wait 3 second
   handler.sendEmptyMessageDelayed(0, 1000);
   bt.setText("Click!");
}

请注意,我确实使用过msg.what这样的标识符,但是您可以创建带有obj参数的消息,以后再使用。

答案 1 :(得分:1)

Java具有Timer类,该类可能会执行您想要的操作。 您可能要使用TimerTask类来生成基本/空任务。 然后使用计时器运行任务。

Baeldung Timer

答案 2 :(得分:0)

尝试一下:

使用postDelayed

 private static int SPLASH_TIME_OUT = 1000;

  if(bt.equal("3")){
        new Handler().postDelayed(new Runnable() {

                @Override
                public void run() {

                // This method will be executed once the timer is over 
                bt.setText("2");
                }
            }, SPLASH_TIME_OUT);  
       }
    ...
    //You can continue like that.

答案 3 :(得分:0)

ClientCache

答案 4 :(得分:0)

使用RxJava可以有效地做到这一点。

Function BuildDataString(rFromRange As Range) As String
Dim sData As String, nrRow As Long, nrCol As Integer, iTotalColumns As Integer

'Convert the input range to a variable and determine the number of columns
vData = rFromRange.Value
iTotalColumns = UBound(vData, 2)


'Loop through all the elements in the array
For nrRow = LBound(vData, 1) To UBound(vData, 1)
    For nrCol = 1 To iTotalColumns
        'Depending on what type of data is encountered either add it to the string or substitute something
        'You'll want to modify this as needed
        If IsError(vData(nrRow, nrCol)) Then
            sData = sData & "Error"
        ElseIf vData(nrRow, nrCol) = "" Or vData(nrRow, nrCol) = 0 Or vData(nrRow, nrCol) = "-" Then
            sData = sData & VBA.Chr$(150)
        Else
            sData = sData & vData(nrRow, nrCol - iIncrement)
        End If

        'Use tab delimiters for Word to know where the columns are
        If nrCol < iTotalColumns Then sData = sData & vbTab
    Next nrCol

    'Add a carriage return for each new line
    If nrRow < UBound(vData, 1) Then sData = sData & vbCr
Next nrRow

'Return the completed string
BuildDataString = sData
End Function