首页 > 代码库 > android延时处理任务范例

android延时处理任务范例

今天要做一个任务,要求图片做按钮开关,点击出发相应事件。点击打开,图片左边显示几行字体,这几行字体是延时显示的。下面将主要代码附上。以下是main.xml

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:orientation="horizontal"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"
    android:layout_weight="6">  
    <LinearLayout 
	    android:orientation="vertical"   
	    android:layout_width="80dp"
	    android:layout_height="fill_parent"
	    android:gravity="center"
	    android:layout_weight="2.5">
    
        <TextView 
            android:id="@+id/txtView01"
            android:layout_width="wrap_content"  
    		android:layout_height="wrap_content"
    		android:text=""
            android:textColor="#ff00"
            android:textSize="20sp"/>
        <TextView 
            android:id="@+id/txtView02"
            android:layout_width="wrap_content"  
    		android:layout_height="wrap_content"
    		android:text=""
    		android:textColor="#999933"
    		android:textSize="20sp"/>
        <TextView 
            android:id="@+id/txtView03"
            android:layout_width="wrap_content"  
    		android:layout_height="wrap_content"
    		android:text=""
    		android:textColor="#ff33cc"
    		android:textSize="20sp"/>
        <TextView 
            android:id="@+id/txtView04"
            android:layout_width="wrap_content"  
    		android:layout_height="wrap_content"
    		android:text=""
    		android:textColor="#993399"
    		android:textSize="20sp"/>
        
    </LinearLayout>
    

    <ImageView    
    	android:id="@+id/start"    
   		android:layout_width="wrap_content"
    	android:layout_height="wrap_content" 
    	android:src=http://www.mamicode.com/"@drawable/blue" >

延时处理要用到以下代码

new Handler().postDelayed(new Runnable(){    
    public void run() {    
    //execute the task    
    }    
 }, delay); 



java代码MainActivity.java如下:

package com.example.vieweffect;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.os.Build;

public class MainActivity extends Activity {
	TextView txtview01;
	TextView txtview02;
	TextView txtview03;
	TextView txtview04;
	
	TextView txtview05;
	TextView txtview06;
	private ImageView start =null;
	protected boolean isBrewing = false; // 按钮置换
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		txtview01=(TextView)findViewById(R.id.txtView01);
		txtview02=(TextView)findViewById(R.id.txtView02);
		txtview03=(TextView)findViewById(R.id.txtView03);
		txtview04=(TextView)findViewById(R.id.txtView04);
		
		txtview05=(TextView)findViewById(R.id.txtView05);
		txtview06=(TextView)findViewById(R.id.txtView06);
		
		start=(ImageView)findViewById(R.id.start);
		start.setOnClickListener((new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				if(v==start){  
		            if(isBrewing)  
		                stopView();  
		            else  
		                startView();  
		              
		        } 
				
			}
		}));
		
	}
	
	//开始  
	public void startView(){  
	Bitmap bmp=BitmapFactory.decodeResource(getResources(), R.drawable.stop);//打开资源图片     
	        start.setImageBitmap(bmp);   
	        txtview01.setText("打开串口...");
	        new Handler().postDelayed(new Runnable(){  
	            public void run() {  
	            //execute the task  
	            	txtview02.setText("打开A...");
	            }  
	         }, 500); 
	        
	        new Handler().postDelayed(new Runnable(){  
	            public void run() {  
	            //execute the task  
	            	txtview03.setText("打开B...");
	            }  
	         }, 1000); 
	        
	        new Handler().postDelayed(new Runnable(){  
	            public void run() {  
	            //execute the task  
	            	txtview04.setText("打开C...");
	            }  
	         }, 1500); 
	        


	        isBrewing = true;  
	}  
	
	
	//停止  
	public void stopView(){  
	Bitmap bmp=BitmapFactory.decodeResource(getResources(), R.drawable.blue);//打开资源图片     
	        start.setImageBitmap(bmp);   
	        txtview01.setText("");
	        txtview02.setText("");
	        txtview03.setText("");
	        txtview04.setText("");
	        
	        txtview05.setText("");
	        txtview06.setText("");
	        isBrewing = false;  
	}  
	
	

	
}

效果图如下:运行后先出现以下界面

,点击该蓝色图片后