zoukankan      html  css  js  c++  java
  • android之消息机制(一)

    消息操作类Handler

    首先编写main.xml文件代码如下:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    	<TextView 
    	    android:id="@+id/info"
    	    android:layout_width="fill_parent"
    	    android:layout_height="wrap_content"/>
    </LinearLayout>
     
    

      然后改写Activity.java类

    代码如下:

    package com.example.myfirstproject;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Timer;
    import java.util.TimerTask;
    
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.app.Activity;
    import android.content.pm.ActivityInfo;
    import android.content.res.Configuration;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup;
    import android.widget.*;
    
    public class MainActivity extends Activity {
    	private static int count = 0;
    	public static final int SET = 1;
    	private Handler myHandler = new Handler(){
    		public void handleMessage(android.os.Message msg){
    			switch(msg.what){
    			case SET:
    				MainActivity.this.info.setText("MLDN-"+count++);
    			}
    		}
    	};
    	private TextView info = null;
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            this.info = (TextView)super.findViewById(R.id.info);
            Timer timer = new Timer();
            timer.schedule(new MyTask(), 0, 1000);
        }    
        private class MyTask extends TimerTask{
        	public void run(){
        		Message msg = new Message();
        		msg.what = SET;
        		MainActivity.this.myHandler.sendMessage(msg);
        	}
        }
    }
    

      运行效果:

    态度决定高度,细节决定成败,
  • 相关阅读:
    ScrollView嵌套EditText联带滑动的解决的方法
    POJ 2003 Hire and Fire (多重链表 树结构 好题)
    leetcode笔记:Bulls and Cows
    PHP中使用ActiveMQ实现消息队列
    WPF模拟键盘输入和删除
    DLLImport的用法C#
    Net Core 的配置模式以及热重载配置
    简体与繁体转换
    Webdings字体、Wingdings字体对照表、用CSS3绘制的各种小图标
    查询大于2分钟的数据
  • 原文地址:https://www.cnblogs.com/lxk2010012997/p/3996192.html
Copyright © 2011-2022 走看看