今天就跟大家聊聊有关怎么在AndroidStudio项目中制作一个倒计时模块,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
创建的activity_main.xml中写入代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:orientation="vertical"
tools:context="cn.edu.gdmec.android.counttime.MainActivity">
<!--填写倒计时时间-->
<EditText
android:id="@+id/input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"/>
<!--获取倒计时时间-->
<Button
android:id="@+id/get"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="获取倒计时时间"/>
<!--显示倒计时-->
<TextView
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<!--开始计时-->
<Button
android:id="@+id/starttime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始计时"/>
<!--停止计时-->
<Button
android:id="@+id/stoptime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止计时"/>
</LinearLayout>
实现功能需求
接下来我们需要在MainActivity.java中现实功能模块需求,主要来显示界面和获取按钮功能效果,代码如下:
package cn.edu.gdmec.android.counttime;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText inputet;
private Button get, startTime, stopTime;
private TextView time;
private int i = 0;
private Timer timer = null;
private TimerTask task = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
inputet = findViewById(R.id.input);
get = findViewById(R.id.get);
startTime = findViewById(R.id.starttime);
stopTime = findViewById(R.id.stoptime);
time = findViewById(R.id.time);
getTime.setOnClickListener(this);
startTime.setOnClickListener(this);
stopTime.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.get:
time.setText(inputet.getText().toString());
i = Integer.parseInt(inputet.getText().toString());
break;
case R.id.starttime:
startTime();
break;
case R.id.stoptime:
stopTime();
break;
default:
break;
}
}
private Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
time.setText(msg.arg1 + "");
startTime();
};
};
public void startTime() {
timer = new Timer();
task = new TimerTask() {
@Override
public void run() {
if (i > 0) { //加入判断不能小于0
i--;
Message message = mHandler.obtainMessage();
message.arg1 = i;
mHandler.sendMessage(message);
}
}
};
timer.schedule(task, 1000);
}
public void stopTime(){
timer.cancel();
}
}
心得重点
//获取的按钮实现:
time.setText(inputet.getText().toString());
i = Integer.parseInt(inputet.getText().toString());
//Handler的加入
private Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
time.setText(msg.arg1 + "");
startTime();
};
};
//倒计时主要核心
public void startTime() {
timer = new Timer();
task = new TimerTask() {
@Override
public void run() {
if (i > 0) { //加入判断不能小于0
i--;
Message message = mHandler.obtainMessage();
message.arg1 = i;
mHandler.sendMessage(message);
}
}
};
timer.schedule(task, 1000);
}
看完上述内容,你们对怎么在AndroidStudio项目中制作一个倒计时模块有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。