2014-01-24 34 views
4

我正在尝试显示延迟的乘法表。我的代码工作正常,但我无法实现延迟。如何在android中设置延迟循环?

这里是我的代码:

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();    
    }  
}); 

任何答案是明显的。

感谢的提前

更新代码: - 该代码正在与@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); 
     } 


    } 


} 

帮忙,谢谢所有的支持者和他们最好的努力帮助我

+1

你想要的乘法开始之间或乘法之前的延迟? – sthor69

+0

我想延迟乘法之间.. – UchihaSasuke

+0

然后你可以使用一个计时器,因为在一些答案 – sthor69

回答

5

你为什么不尝试一下其他与此毫不费力说;)

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); 

     } 


    } 


} 
+1

伟大的努力人! ü值得最好.. :) – UchihaSasuke

+0

不客气:) – TheLittleNaruto

+0

我需要一些额外的帮助来解决这个问题如何获得数字之间的延迟。 – UchihaSasuke

1

添加一个处理程序()。替换为您的onClick代码:你希望它在毫秒延迟时间

final Handler handler = new Handler(); 
     handler.postDelayed(new Runnable() { 
      @Override 
      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); 
        } 
    }, 5000); 

Repalce 5000

+0

欢迎!!我试过它工作正常,但我想延迟我的乘法表内。 – UchihaSasuke

1

添加Handler将执行您的可运行:

Runnable runnable = new Runnable() { 
    @Override 
    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"); 
//--ADDED stuff here------------------------------------------------------------ 
      try { 
       //Sleep will suspend your Thread for 500 miliseconds and resumes afterwards 
       Thread.sleep(500); 
      } catch (InterruptedException e) { 
       Log.e("error, Thread interrupted", e); 
      } 
     } 
     s = String.valueOf(sb); 

     Intent intent=new Intent(getApplicationContext(),TextViewActivity.class); 
     intent.putExtra("MA", s); 
     startActivity(intent); 
    } 
Handler handler = new Handler(); 
//this will execute your runnable after 500 milliseconds 
handler.postDelayed(runnable, 500); 
+0

欢迎!!我之前尝试过,但我可以延迟在我的表的开始,但我想延迟我的乘法表内。 – UchihaSasuke

+0

@Chandan:我在代码中添加了一个编辑来延迟循环中的每一步。 – Simulant

+0

我试图实现我的点击按钮,但正如我使用这一行handler.postDelayed(runnable,500);它开始给我错误..所有线是红色的。 – UchihaSasuke

0

您可以使用即:

public class Scheduler { 

// DATA 
private OnScheduleTimeListener mListener; 
private Handler     mHandler; 
private int      mInterval;   // Between each executions 
private static final int  DELAY = 100;  // before first execution 
private boolean     mIsTimerRunning; 

public static interface OnScheduleTimeListener { 

    public void onScheduleTime(); 
} 

public Scheduler(int interval) { 
    super(); 
    mInterval = interval; 
    mHandler = new Handler(); 
} 

private final Runnable mRunnable = new Runnable() { 

             @Override 
             public void run() { 
              // Do stuff 
              mListener.onScheduleTime(); 
              // Repeat 
              mHandler.postDelayed(mRunnable, mInterval); 
             } 
            }; 

public void setOnScheduleTimeListener(OnScheduleTimeListener listener) { 
    mListener = listener; 
} 

public void startTimer() { 
    mIsTimerRunning = true; 
    mHandler.postDelayed(mRunnable, DELAY); 
} 

public void stopTimer() { 
    mIsTimerRunning = false; 
    mHandler.removeCallbacks(mRunnable); 
} 

public boolean isTimerRunning() { 
    return mIsTimerRunning; 
} 
} 

现在就使用它:

private void startTimer() { 
     mScheduler = new Scheduler(INTERVAL); 
     mScheduler.setOnScheduleTimeListener(new OnScheduleTimeListener() { 

     @Override 
     public void onScheduleTime() { 
       Log.d(TAG, "update"); 
     }); 
     mScheduler.startTimer(); 
} 

private void stopTimer(){ 
    if (mScheduler != null && mScheduler.isTimerRunning()) { 
     mScheduler.stopTimer(); 
     mScheduler = null; 
    } 
} 
+0

你能帮助我如何使用此代码。 – UchihaSasuke

+0

这是一个简单的解决方案,只使用postDelayed不冻结线程。我使用它作为每30秒的小UI更新(延迟和时间全部以毫秒为单位) –

2

试试这个

Timer timer = new Timer(); 
timer.schedule(new TimerTask() { 
    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"); 
     } 
    }, 5000); 
s = String.valueOf(sb); 

Intent intent=new Intent(getApplicationContext(),TextViewActivity.class); 
intent.putExtra("MA", s); 
startActivity(intent); 
0

试试这个....

do { 
     try { 
      try { 
       response_req_sequence = SimpleHttpClient 
         .sendresponseSequReqRes(response_send_order); 
       System.out.println("response of sequence request" 
         + response_req_sequence); 
       System.out.println(" i ma in thread"); 
       if (response_req_sequence.trim().length() != 0) { 
        System.out.println("response in result of sequ"+ response_req_sequence); 
        break; 
       } 
       Thread.sleep(10000); 
      } catch (Exception e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } catch (Exception e) { 
      // TODO: handle exception 
     } 
    } while (response_req_sequence.trim().equalsIgnoreCase("")); 

这是对我工作的罚款。你可以根据你自定义。