版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】怎么在Android中通过自定义TimeButton实现倒计时按钮
这期内容当中在下将会给大家带来有关怎么在Android中通过自定义TimeButton实现倒计时按钮,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。首先新建一个App.class继承于Applicationpackage
com.example.xuboyu.myapplication;
import
java.util.Map;
import
android.app.Application;
public
class
App
extends
Application
{
//
用于存放倒计时时间
public
static
Map<String,
Long>
map;
}然后编写TimeButton.class继承于Buttonpackage
com.example.xuboyu.myapplication;
import
java.util.HashMap;
import
java.util.Map;
import
java.util.Timer;
import
java.util.TimerTask;
import
android.annotation.SuppressLint;
import
android.content.Context;
import
android.os.Bundle;
import
android.os.Handler;
import
android.util.AttributeSet;
import
android.util.Log;
import
android.view.View;
import
android.view.View.OnClickListener;
import
android.widget.Button;
/**
*
倒计时按钮
*
@author
bnuzlbs-xuboyu
2017/4/5.
*
注意把该类的onCreate()onDestroy()和activity的onCreate()onDestroy()同步处理
*/
public
class
TimeButton
extends
Button
implements
OnClickListener
{
private
long
lenght
=
60
*
1000;//
倒计时长度,这里给了默认60秒
private
String
textafter
=
"秒后重新获取~";
private
String
textbefore
=
"点击获取验证码~";
private
int
colorafter;
private
int
colorbefore;
private
final
String
TIME
=
"time";
private
final
String
CTIME
=
"ctime";
private
OnClickListener
mOnclickListener;
private
Timer
t;
private
TimerTask
tt;
private
long
time;
Map<String,
Long>
map
=
new
HashMap<String,
Long>();
public
TimeButton(Context
context)
{
super(context);
setOnClickListener(this);
}
public
TimeButton(Context
context,
AttributeSet
attrs)
{
super(context,
attrs);
setOnClickListener(this);
}
@SuppressLint("HandlerLeak")
Handler
han
=
new
Handler()
{
public
void
handleMessage(android.os.Message
msg)
{
TimeButton.this.setText(time
/
1000
+
textafter);
time
-=
1000;
if
(time
<
0)
{
TimeButton.this.setEnabled(true);
TimeButton.this.setText(textbefore);
clearTimer();
}
};
};
private
void
initTimer()
{
time
=
lenght;
t
=
new
Timer();
tt
=
new
TimerTask()
{
@Override
public
void
run()
{
Log.e("xuboyu",
time
/
1000
+
"");
han.sendEmptyMessage(0x01);//十六进制的数字1
}
};
}
private
void
clearTimer()
{
if
(tt
!=
null)
{
tt.cancel();
tt
=
null;
}
if
(t
!=
null)
t.cancel();
t
=
null;
}
@Override
public
void
setOnClickListener(OnClickListener
l)
{
if
(l
instanceof
TimeButton)
{
super.setOnClickListener(l);
}
else
this.mOnclickListener
=
l;
}
@Override
public
void
onClick(View
v)
{
if
(mOnclickListener
!=
null)
mOnclickListener.onClick(v);
initTimer();
this.setText(time
/
1000
+
textafter);
this.setEnabled(false);
t.schedule(tt,
0,
1000);
//
t.scheduleAtFixedRate(task,
delay,
period);
}
/**
*
和activity的onDestroy()方法同步
*/
public
void
onDestroy()
{
if
(App.map
==
null)
App.map
=
new
HashMap<String,
Long>();
App.map.put(TIME,
time);
App.map.put(CTIME,
System.currentTimeMillis());
clearTimer();
Log.e("xuboyu",
"onDestroy");
}
/**
*
和activity的onCreate()方法同步
*/
public
void
onCreate(Bundle
bundle)
{
Log.e("xuboyu:倒计时相关",
App.map
+
"");
if
(App.map
==
null)
return;
if
(App.map.size()
<=
0)//
这里表示没有上次未完成的计时
return;
long
time
=
System.currentTimeMillis()
-
App.map.get(CTIME)
-
App.map.get(TIME);
App.map.clear();
if
(time
>
0)
return;
else
{
initTimer();
this.time
=
Math.abs(time);
t.schedule(tt,
0,
1000);
this.setText(time
+
textafter);
this.setEnabled(false);
}
}
/**
*
设置计时时候显示的文本
*/
public
TimeButton
setTextAfter(String
text1)
{
this.textafter
=
text1;
return
this;
}
/**
*
设置点击之前的文本
*/
public
TimeButton
setTextBefore(String
text0)
{
this.textbefore
=
text0;
this.setText(textbefore);
return
this;
}
/**
*
设置到计时长度
*
@param
lenght
*
时间
默认毫秒
*
@return
*/
public
TimeButton
setLenght(long
lenght)
{
this.lenght
=
lenght;
return
this;
}
}最后在MainActivity.class中调用package
com.example.xuboyu.myapplication;
import
android.app.Activity;
import
android.os.Bundle;
import
android.view.View;
import
android.view.View.OnClickListener;
import
android.widget.Toast;
/**
*
测试主界面
*
@author
bnuzlbs-xuboyu
2017/4/5.
*/
public
class
MainActivity
extends
Activity
implements
OnClickListener
{
private
TimeButton
v;
private
TimeButton
v2;
private
TimeButton
v3;
@Override
protected
void
onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
v
=
(TimeButton)
findViewById(R.id.button1);
v.onCreate(savedInstanceState);
v.setTextAfter("秒后重新排队").setTextBefore("点击开始排队").setLenght(15
*
1000);
v.setOnClickListener(this);
v2
=
(TimeButton)
findViewById(R.id.button2);
v2.onCreate(savedInstanceState);
v2.setTextAfter("秒后重新验证").setTextBefore("点击发送验证码").setLenght(10
*
1000);
v2.setOnClickListener(this);
v3
=
(TimeButton)
findViewById(R.id.button3);
v3.onCreate(savedInstanceState);
v3.setTextAfter("秒后重新倒计时").setTextBefore("点击开始倒计时").setLenght(5
*
1000);
v3.setOnClickListener(this);
}
@Override
public
void
onClick(View
v)
{
//
TODO
Auto-generated
method
stub
Toast.makeText(MainActivity.this,
"这是处理调用者onclicklistnenr",
Toast.LENGTH_SHORT).show();
}
@Override
protected
void
onDestroy()
{
//
TODO
Auto-generated
method
stub
v.onDestroy();
v2.onDestroy();
super.onDestroy();
}
}其中绿色按钮是使用了自定义样式的Button,使用起来也很简单首先在drawable中新建一个样式文件mybutton.xml<?xml
version="1.0"
encoding="utf-8"?>
<shape
xmlns:android="/apk/res/android">
<solid
android:color="#5cbe6c"
/>
<!--
设置按钮的四个角为弧形
-->
<!--
android:radius
弧形的半径
-->
<corners
android:radius="15dip"
/>
<!--
padding:Button里面的文字与Button边界的间隔
-->
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp"
/>
</shape>然后在定义TimeButton的时候如下:android:background="@drawable/mybutton"<com.example.xuboyu.myapplication.TimeButton
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 竞业协议和竞业协议书 竞业协议合同6篇
- 2022-2023学年江西省上饶市枫港中学高一数学文联考试题含解析
- 品转租合同范本6篇
- 2022年湖北省宜昌市宜都第三高级中学外国语学校高一数学文下学期摸底试题含解析
- 湖南省岳阳市临湘第二中学高一数学文模拟试卷含解析
- 广东省揭阳市揭西第三华侨中学高一数学文上学期期末试卷含解析
- 2022年广东省佛山市第五高级中学高一数学文月考试题含解析
- 广东省韶关市仁化中学2022-2023学年高一数学文模拟试题含解析
- 广东省东莞市湖景中学高一英语上学期摸底试题含解析
- 东方甄选董宇辉爆火知识营销
- 中国大唐集团公司光伏项目精品工程创建及评价管理办法
- 特种设备交接班管理制度管理办法
- 新大学法语I教案——中国海洋大学法语教案
- 山东省会计事务所收费标准
- 浅谈幼儿园自制玩教具的开发与应用开题
- 3.变压器安装合同(范本)
- 【整理】挡土墙的设计步骤
- 部编版小学三年级语文下册《习作-国宝大熊猫》PPT课件
- 公司分立合同范本4篇
- 高等代数II期末考试试卷及答案A卷
- “列方程解应用题”教学反思
评论
0/150
提交评论