今天要做一个任务,要求图片做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;
} }

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

,点击该蓝色图片后

android延时处理任务范例的更多相关文章

  1. Android.mk编译APK范例

    以下仅是使用Android.mk编译APK程序的一些范例. 如果你想了解Android.mk的更多内容请参考<Android.mk简介> 一.编译一个简单的APK    LOCAL_PAT ...

  2. XE7 Android 中使用 MessageDlg 范例

    MessageDlg('Choose a button:', System.UITypes.TMsgDlgType.mtInformation, [ System.UITypes.TMsgDlgBtn ...

  3. Android 延时执行任务的三种简单方法

    开启一个新的线程 new Thread() { @Override public void run() { try { Thread.sleep(2000); } catch (Interrupted ...

  4. Android延时执行调用的几种方法

    一.开启新线程 new Thread(new Runnable(){        public void run(){            Thread.sleep(XXXX);          ...

  5. Android延时执行的几种方法

    开启新线程 new Thread(new Runnable(){ public void run(){ Thread.sleep(XXXX); handler.sendMessage(); //告诉主 ...

  6. android延时弹出软键盘

    searchEditView.setFocusable(true); searchEditView.setFocusableInTouchMode(true); searchEditView.requ ...

  7. Android 延时执行的几种方法

    开启新线程 new Thread(new Runnable(){ public void run(){ Thread.sleep(XXXX); handler.sendMessage(); //告诉主 ...

  8. 【译】Design For Maturing Android-为日渐成熟的Android做设计

    为日渐成熟的Android做设计 [声明] 本篇博文英文版原文来自Smashing Magazine,本人经Smashing Magazine邮件授权后翻译,转载请注明出处.谢谢!原文地址. 我将不定 ...

  9. 【整理修订】Android.mk详解

    Android.mk详解 1. Android.mk 的应用范围 Android.mk文件是GNU Makefile的一小部分,它用来对Android程序进行编译. 一个Android.mk文件可以编 ...

随机推荐

  1. 【42.86%】【Codeforces Round #380D】Sea Battle

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  2. GAS Syntax

    GAS or GNU as syntax is a different form of syntax for assembly language files, known also as AT& ...

  3. 王立平--eclipse本地配置svn

    1.下载 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzQyNTUyNw==/font/5a6L5L2T/fontsize/400/fill/I ...

  4. Delphi 获取Internet缓存文件 -- FindFirstUrlCacheEntry FindNextUrlCacheEntry

    下面是我写的一个函数,把所有的缓存文件路径添加到一个字符串列表中,直接看代码,带了注释.另外还有删除缓存等等大家自己到msdn找找. 需要引用 WinInet // 获取Internet缓存文件 fu ...

  5. dmalloc arm-linux平台使用

    话说“工欲善其事,必先得其器”,用C语言写程序,最怕遇到个什么内存泄漏,内存越界访问了,心里那个急啊... 如果在i368-linlux上,valgrind工具是首选,但在arm-linux平台上,如 ...

  6. ExtJS中store.findExact

    var ds = myGrid.apf_ds; var store = myGrid.getStore(); forEach(data, function (item) { if (store.fin ...

  7. Android中的动画详解系列【2】——飞舞的蝴蝶

    这一篇来使用逐帧动画和补间动画来实现一个小例子,首先我们来看看Android中的补间动画. Android中使用Animation代表抽象的动画类,该类包括下面几个子类: AlphaAnimation ...

  8. Android中HandlerThread的使用及源代码解析

    关于Hanlder的基本使用能够參见博文<Android中Handler的使用>,假设想了解Handler.Looper.Thread等的相互关系以及内部实现原理能够參见博文<深入源 ...

  9. [Grid Layout] Use auto-fill and auto-fit if the number of repeated grid tracks is not to be def

    What about the situation in which we aren’t specifying the number of columns or rows to be repeated? ...

  10. Java中sleep()与wait()区别

    学习时正好碰到这两个方法,就查阅相关资料,并通过程序实现,进行区别一下: 1.每个对象都有一个锁来控制同步访问,Synchronized关键字可以和对象的锁交互,来实现同步方法或同步块.sleep() ...