Android开发--倒计时的实现
来源:互联网 发布:树熊网络wifi如何登录 编辑:程序博客网 时间:2024/05/22 02:16
Android开发中,很多时候我们需要对重新发送这种的按钮上面实现一个倒计时的功能。
这时候,Android给我们提供了一个非常不错的倒计时类:CountDownTimer
首先,我们需要在布局文件当中添加一个Textview和一个Button控件
如下:
<span style="font-size:18px;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.hql.timelater.MainActivity" > <TextView android:id="@+id/tmelater" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/timelater" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="100dp" android:enabled="false" android:text="@string/hello_world" /></RelativeLayout></span>
然后在MainActivity.java文件添加代码
public class MainActivity extends Activity{private TextView tv_showTime;private CountDownTimer time;private Button btn_onclick;protected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);tv_showTime = (TextView) findViewById(R.id.tmelater);btn_onclick = (Button) findViewById(R.id.timelater);time = new CountDownTimer(60000, 1000){public void onTick(long millisUntilFinished){tv_showTime.setText("seconds remaining: " + millisUntilFinished / 1000);btn_onclick.setText("剩余" + millisUntilFinished / 1000 + "s");}public void onFinish(){tv_showTime.setText("done!");btn_onclick.setText("done!");btn_onclick.setEnabled(true);}}.start();}}
注意
1、time=new CountDownTimer(60000,1000)参数解释:第一个参数,时间总长度。第二个参数,间隔时间长度。单位:毫秒
2、millisUntilFinished:表示总时间
0 0
- Android开发--倒计时的实现
- android开发中倒计时功能的实现
- Android开发中倒计时功能的实现
- Android开发-03-倒计时功能的实现
- android开发 倒计时和计时的实现
- Android 倒计时的实现
- Android -- 倒计时的实现
- 【Android】Android开发小功能,倒计时的实现。时间计时器倒计时功能。
- Android控件倒计时的实现
- Android倒计时功能的实现
- Android倒计时的实现代码
- Android 酷炫 倒计时的实现
- Android倒计时原理的实现
- Android倒计时控件的实现
- Android 倒计时功能的实现
- Android实现倒计时的按钮
- android倒计时功能的实现
- Android实现倒计时的按钮
- android 获取布局填充器的三种方式 LayoutInflater
- 【HTML5】Canvas基础知识讲解
- struts2的Action Chaining
- Git 的基本使用步骤
- MyBatis 一对多双向关联查询
- Android开发--倒计时的实现
- EJB3.x:部署(卸载)EntityBean时自动创建(删除)表结构
- struts2的ActionEventListener
- JavaScript 对象的创建
- ListView的新朋友-------RecyclerView初识
- Q9.4 Write a method that returns all subsets of a set.
- libcurl 使用及例程
- 数组名和数组名取地址的区别
- C#中转义符和@符号的作用