メッセージ操作クラスHandler
1399 ワード
public class FirstActivity extends AppCompatActivity {
private static int count = 0;
public static final int SET = 1; // what
private Handler myHandle = new Handler() { // Handler
@Override
public void handleMessage(Message msg) {
switch (msg.what) { //
case SET:
FirstActivity.this.info.setText("dailei---"+count++);
}
super.handleMessage(msg);
}
};
private TextView info = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_main);
this.info = (TextView)findViewById(R.id.info);
Timer timer = new Timer(); //
timer.schedule(new MyTask(),0,1000); // , 1
}
/**
*
*/
private class MyTask extends TimerTask{
@Override
public void run() {
Message msg = new Message(); // message
msg.what = SET; //
FirstActivity.this.myHandle.sendMessage(msg); //
}
}
}
2. activity_main.xml
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/info"/>