要做到每一秒

时间:2014-03-11 07:28:18

标签: android button timer

这是我的代码:

Input = (EditText) findViewById(R.id.inputText);
Button Button = (Button) findViewById(R.id.Button);
Button.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View view) {
        Toast.makeText(getApplicationContext(),"Working..", Toast.LENGTH_LONG).show();  
        InputString = Input.getText().toString();
        InputArr = InputString.split(" ");
        L=InputArr.length;
        while (L!=0)
        {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block 
                e.printStackTrace();
            }

            Toast.makeText(getApplicationContext(),InputArr[L-1], Toast.LENGTH_SHORT).show();  
            Output.setText(InputArr[L-1]);
            L--;
        }
    }
);

我想每秒将输出文本设置为InputArr[L-1],但是当我运行应用程序并单击按钮时,我只看到InputArr[1]而不是所有进程,为什么?

2 个答案:

答案 0 :(得分:0)

现在开始,但首先看一下CountDownTimer

创建另一种方法,您将添加计时器

private void xyz(String[] InputArr){
    int L=0;
    CountDownTimer cdt= new CountDownTimer(30000, 1000) {

      public void onTick(long millisUntilFinished) {
        Output.setText(InputArr[L]);
        L++;
      }

   }.start();


 }

并在onClick方法中调用此方法,如此

public void onClick(View view) {
    Toast.makeText(getApplicationContext(),"Working..", Toast.LENGTH_LONG).show();  
    InputString = Input.getText().toString();
    InputArr = InputString.split(" ");
    xyz(InputArr);
}

答案 1 :(得分:0)

会,这不起作用:/ 这是代码: 那里有很多错误..

 private void T( String[] InputArr)
        {
            int L=0;
            int N=InputArr.length;
            CountDownTimer cdt= new CountDownTimer(N,1000){

             public void onTick(long millisUntilFinished) {
             Output.setText(InputArr[L]);
             L++;
             }

           }.start();


         }

        Button   Button = (Button) findViewById(R.id.Button);
        Button.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View view) {
         Toast.makeText(getApplicationContext(),"Working..", Toast.LENGTH_LONG).show();  
          InputString = Input.getText().toString();
           InputArr = InputString.split(" ");
           T(InputArr);
            }


        });   

我的问题是什么?