首页 > 安全资讯 >

android延时处理任务范例

14-06-20

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

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

 

延时处理要用到以下代码

 

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;  
	}  
	
	

	
}

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

 

height=252,点击该蓝色图片后height=248height=248

相关文章
最新文章
热点推荐