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;
} }
效果图例如以下:执行后先出现下面界面
,点击该蓝色图片后
,
android延时处理任务范例的更多相关文章
- Android.mk编译APK范例
以下仅是使用Android.mk编译APK程序的一些范例. 如果你想了解Android.mk的更多内容请参考<Android.mk简介> 一.编译一个简单的APK LOCAL_PAT ...
- XE7 Android 中使用 MessageDlg 范例
MessageDlg('Choose a button:', System.UITypes.TMsgDlgType.mtInformation, [ System.UITypes.TMsgDlgBtn ...
- Android 延时执行任务的三种简单方法
开启一个新的线程 new Thread() { @Override public void run() { try { Thread.sleep(2000); } catch (Interrupted ...
- Android延时执行调用的几种方法
一.开启新线程 new Thread(new Runnable(){ public void run(){ Thread.sleep(XXXX); ...
- Android延时执行的几种方法
开启新线程 new Thread(new Runnable(){ public void run(){ Thread.sleep(XXXX); handler.sendMessage(); //告诉主 ...
- android延时弹出软键盘
searchEditView.setFocusable(true); searchEditView.setFocusableInTouchMode(true); searchEditView.requ ...
- Android 延时执行的几种方法
开启新线程 new Thread(new Runnable(){ public void run(){ Thread.sleep(XXXX); handler.sendMessage(); //告诉主 ...
- 【译】Design For Maturing Android-为日渐成熟的Android做设计
为日渐成熟的Android做设计 [声明] 本篇博文英文版原文来自Smashing Magazine,本人经Smashing Magazine邮件授权后翻译,转载请注明出处.谢谢!原文地址. 我将不定 ...
- 【整理修订】Android.mk详解
Android.mk详解 1. Android.mk 的应用范围 Android.mk文件是GNU Makefile的一小部分,它用来对Android程序进行编译. 一个Android.mk文件可以编 ...
随机推荐
- Spring Boot初步认识
Spring Boot 来源及背后 Spring Boot开发始于 2013 年,伴随Spring4.0而生,2014 年 4 月发布 1.0.0 版本.当前版本1.4.0,http://projec ...
- [Javascript] Validate Data with the Every() Method
The every method returns true or false based on whether or not every item in the array passes the co ...
- stm32 DMA+timer+DAC
是有延迟的:
- composer 安装 laravel 更换下载源
1.在确认cmd 下载好composer 2. composer -V 3. composer config -g repo.packagist composer https://packagist. ...
- 今天竟然有人模仿我的QQ号进行骗钱
今天下午,CoderGeek同学告诉我,有个叫"小雷FansUnion"的QQ正在找他要钱,他直接和我这个正宗的"小雷FansUnion"探听虚实.这时才发现, ...
- Lucene学习总结之一:全文检索的基本原理 2014-06-25 14:11 666人阅读 评论(0) 收藏
一.总论 根据http://lucene.apache.org/java/docs/index.html 定义: Lucene 是一个高效的,基于Java 的全文检索库. 所以在了解Lucene之前要 ...
- js中ajax连接服务器open函数的另外两个默认参数get请求和默认异步(open的post方式send函数带参数)(post请求和get请求区别:get:快、简单 post:安全,量大,不缓存)(服务器同步和异步区别:同步:等待服务器响应当中浏览器不能做别的事情)(ajax和jquery一起用的)
js中ajax连接服务器open函数的另外两个默认参数get请求和默认异步(open的post方式send函数带参数)(post请求和get请求区别:get:快.简单 post:安全,量大,不缓存)( ...
- Android String与十六进制数互转
/** * 字符串转换成十六进制字符串 * @param String str 待转换的ASCII字符串 * @return String 每个Byte之间空格分隔,如: [61 6C 6B] */ ...
- 搭建微信小程序开发环境
1.下载开发工具 点击进入下载地址选择和自己电脑匹配的安装包,并安装: image.png 安装完成后出现应用icon: image.png 2.创建项目 能够扫码登录的前提是微信号已经注册了小程序, ...
- Opencv中使用Surf特征实现图像配准及对透视变换矩阵H的平移修正
图像配准需要将一张测试图片按照第二张基准图片的尺寸.角度等形态信息进行透视(仿射)变换匹配,本例通过Surf特征的定位和匹配实现图像配准. 配准流程: 1. 提取两幅图像的Surf特征 2. 对Sur ...