android延时处理任务范例

今天要做一个任务,要求图片做button开关,点击出发对应事件。点击打开,图片左边显示几行字体,这几行字体是延时显示的。以下将主要代码附上。以下是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="@drawable/blue" 
    	android:layout_gravity="center" 
    	android:layout_weight="1"
    /> 
    
    <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/txtView05"
            android:layout_width="wrap_content"  
    		android:layout_height="wrap_content"
    		android:text=""
    		android:textColor="#ff6666"
            android:textSize="20sp"
            />
        
        <TextView 
            android:id="@+id/txtView06"
            android:layout_width="80dp"  
    		android:layout_height="wrap_content"
    		android:text=""
    		android:textColor="#003366"
            android:textSize="20sp"
            />
        
        
     </LinearLayout>
    
</LinearLayout>  


延时处理要用到下面代码

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; // button置换
	@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;  
	}  
	
	

	
}

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

技术分享,点击该蓝色图片后技术分享技术分享

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。