这个需要在输出,点击start后会隔一会输出一行字,点end会结束输出。
Codepackage tk.handleractivity;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MyHangler extends Activity { private Button startButton=null; private Button endButton=null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.myhangler); startButton=(Button)findViewById(R.id.startButton); startButton.setOnClickListener(new StartbuttonListener()); endButton=(Button)findViewById(R.id.endButton); endButton.setOnClickListener(new EndbuttonListener()); } class StartbuttonListener implements OnClickListener{ @Override public void onClick(View v) { handler.post(updateThead); } } class EndbuttonListener implements OnClickListener{ @Override public void onClick(View v) { handler.removeCallbacks(updateThead); } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.myhangler, menu); return true; } Handler handler=new Handler(); Runnable updateThead=new Runnable() { @Override public void run() { System.out.println("UpdateThead"); handler.postDelayed(updateThead, 3000); } };}