package com.example.imageview;
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;
// Handler常用方法:
// 1 post()
// 2 postDelayed()
// 3 sendMessage()
// 4 handleMessage()
public class MyHandlerPostDelayed extends Activity {
int i = 0;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textView1);
handler.post(run);
// handler.postDelayed(run, 1000);
}
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
String s = String.valueOf(msg.arg1);
tv.setText("msg.arg1---" + s);
}
};
Runnable run = new Runnable() {
@Override
public void run() {
Message message = new Message();
i++;
message.arg1 = i;
Log.i("test", "计数---" + i);
handler.sendMessage(message);
handler.postDelayed(this, 1000);//回调,没有写的话只执行一次,可以实现计时功能。
// if (i == 100) {//结束回调
// handler.removeCallbacks(run);
// }
}
};
}
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;
// Handler常用方法:
// 1 post()
// 2 postDelayed()
// 3 sendMessage()
// 4 handleMessage()
public class MyHandlerPostDelayed extends Activity {
int i = 0;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textView1);
handler.post(run);
// handler.postDelayed(run, 1000);
}
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
String s = String.valueOf(msg.arg1);
tv.setText("msg.arg1---" + s);
}
};
Runnable run = new Runnable() {
@Override
public void run() {
Message message = new Message();
i++;
message.arg1 = i;
Log.i("test", "计数---" + i);
handler.sendMessage(message);
handler.postDelayed(this, 1000);//回调,没有写的话只执行一次,可以实现计时功能。
// if (i == 100) {//结束回调
// handler.removeCallbacks(run);
// }
}
};
}