Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android延時處理任務范例

android延時處理任務范例

編輯:關於Android編程

今天要做一個任務,要求圖片做按鈕開關,點擊出發相應事件。點擊打開,圖片左邊顯示幾行字體,這幾行字體是延時顯示的。下面將主要代碼附上。以下是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

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved