如何设置延时循环中的android? [英] How to set Delay for loop in android?

查看:1011
本文介绍了如何设置延时循环中的android?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想延迟显示乘法表。我的code工作正常,但我不能够实现的延迟。

I am trying to display a multiplication table with delay. My code is working fine but I am not able to implement the delay.

下面是我的code:

tableButton1 = (Button) findViewById(R.id.Button1);
tableButton1.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {       
        new Thread(new Runnable() {
             public void run() {
                    str = tableButton1.getText().toString();  
                     a = Integer.parseInt(str);
                     StringBuilder sb = new StringBuilder();
                     for (int i = 1; i <= 10; i++){
                        sb.append(a + " x " + i + " = " + i * a+ "\n");

                     }
                     s = String.valueOf(sb);

                    Intent intent=new Intent(getApplicationContext(),TextViewActivity.class);
                    intent.putExtra("MA", s);
                    startActivity(intent);
             }
         });
         //Toast.makeText(getApplicationContext(), "Hi"  +ss, 222).show();              
    }       
}); 

任何的答案是AP preciable。

Any answer is appreciable.

感谢的提前

Updated- code : - 这code正在与@theLittleNaruto帮助

Updated-code:- This code is working with help of @theLittleNaruto

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class TestActivity  extends Activity{
Button tableButton1;
TextView txtView; 

int value = 0; 
    static int count = 0;
    Handler handle = new Handler();

    StringBuilder sb = new StringBuilder();

    Runnable r  = new Runnable() {

        @Override
        public void run() {
            Toast.makeText(getApplicationContext(), "onrunnable" +sb, 222).show();
            // TODO Auto-generated method stub
            updateTable();
        }
    };

        @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
                setContentView(R.layout.text_display);

                Toast.makeText(getApplicationContext(), "oncreate" , 222).show();
                txtView = (TextView) findViewById(R.id.outputTXT);
               tableButton1 = (Button) findViewById(R.id.seven);
               tableButton1.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {       

                Toast.makeText(getApplicationContext(), "onclick" , 222).show();
                value= Integer.parseInt(tableButton1.getText().toString()); 
                updateTable();

            }
       });
      }


     public void updateTable(){

        count+=1000;
        if(count==11000){
            //Toast.makeText(getApplicationContext(), "onupdate" , 222).show();
            count = 0;
            value=0;
            handle.removeCallbacks(r);
            sb.setLength(0);

        }else{

            sb.append(value + " x " + count/1000 + " = " + count/1000 * value+ "\n");
                        handle.postDelayed(r, 1000);
                       // Toast.makeText(getApplicationContext(), "onupdateElse" +sb, 222).show();
                        txtView.setText(sb);
        }


    }


}

感谢您所有的支持者和他们的最好的尝试,以帮助我

Thank you all the supporters and their best try to help me

推荐答案

你为什么不尝试一下另一种是说这个小的努力;)

Why dont you try what the other is saying with this little effort ;)

public class TestActivity extends Activity{

int value = 0; 
    static int count = 0;
    Handler handle = new Handler();

    StringBuilder sb = new StringBuilder();

    Runnable r  = new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            updateTable();
        }
    };

        @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
                setContentView(R.layout.oaot_get);


               tableButton1.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {       


                value= Integer.parseInt(tableButton1.getText().toString()); 
                updateTable();

            }
       });
      }


     public void updateTable(){

        count+=1000;
        if(count==11000){

            count = 0;
            value=0;
            handle.removeCallbacks(r);
            sb.setLength(0);

        }else{

            sb.append(value + " x " + count/1000 + " = " + count/1000 * value+ "\n");
                        handle.postDelayed(r, 1000);

        }


    }


}

这篇关于如何设置延时循环中的android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆