问题描述
我试图建立一个倒数计时器的机器人。在这个例子中我输入号码分钟作为一个参数,该参数表示在倒数计时器的
现在的问题是,当我进入分钟,例如65,则计数器会65:00。我想柜台展示1:05:00即在HH:MM:SS格式
公共类MainActivity扩展活动实现OnClickListener {
私人按钮buttonStartTime,buttonStopTime;
私人的EditText edtTimerValue;
私人TextView的textViewShowTime; //将显示时间
私人CountDownTimer countDownTimer; //建于安卓类
// 倒计时器
专用长totalTimeCountInMilliseconds; //总倒计时时间
//毫秒
专用长timeBlinkInMilliseconds; //开始启动的时候闪烁
私人布尔眨眼; //控制开和关的闪烁..
/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
buttonStartTime =(按钮)findViewById(R.id.btnStartTime);
buttonStopTime =(按钮)findViewById(R.id.btnStopTime);
textViewShowTime =(TextView中)findViewById(R.id.tvTimeCount);
edtTimerValue =(EditText上)findViewById(R.id.edtTimerValue);
buttonStartTime.setOnClickListener(本);
buttonStopTime.setOnClickListener(本);
}
@覆盖
公共无效的onClick(视图v){
如果(v.getId()== R.id.btnStartTime){
textViewShowTime.setTextAppearance(getApplicationContext(),
R.style.normalText);
SetTimer的();
buttonStopTime.setVisibility(View.VISIBLE);
buttonStartTime.setVisibility(View.GONE);
edtTimerValue.setVisibility(View.GONE);
edtTimerValue.setText();
startTimer();
}否则,如果(v.getId()== R.id.btnStopTime){
countDownTimer.cancel();
buttonStartTime.setVisibility(View.VISIBLE);
buttonStopTime.setVisibility(View.GONE);
edtTimerValue.setVisibility(View.VISIBLE);
}
}
私人无效SetTimer的(){
INT时间= 0;
如果(!edtTimerValue.getText()。的toString()。等于()){
时间=的Integer.parseInt(edtTimerValue.getText()的toString());
} 其他
Toast.makeText(MainActivity.this,请输入分钟......,
Toast.LENGTH_LONG).show();
totalTimeCountInMilliseconds = 60 *时* 1000;
timeBlinkInMilliseconds = 30 * 1000;
}
私人无效startTimer(){
countDownTimer =新CountDownTimer(totalTimeCountInMilliseconds,500){
// 500的手段,onTick函数被调用每500
//毫秒
@覆盖
公共无效onTick(长leftTimeInMilliseconds){
长秒= leftTimeInMilliseconds / 1000;
如果(leftTimeInMilliseconds< timeBlinkInMilliseconds){
textViewShowTime.setTextAppearance(getApplicationContext(),
R.style.blinkText);
//改变TextView的风格..给人一种红
//警报风格
如果(闪烁){
textViewShowTime.setVisibility(View.VISIBLE);
//如果眨眼是真实的,TextView的将是可见
} 其他 {
textViewShowTime.setVisibility(View.INVISIBLE);
}
闪烁=闪烁!; //切换闪烁的价值
}
textViewShowTime.setText(的String.Format(%02D秒/ 60)
+:+的String.Format(%02D,秒%60));
//格式化的TextView显示易于读取的格式
}
@覆盖
公共无效onFinish(){
//当timecount完成这个函数会被调用
textViewShowTime.setText(时间了!);
textViewShowTime.setVisibility(View.VISIBLE);
buttonStartTime.setVisibility(View.VISIBLE);
buttonStopTime.setVisibility(View.GONE);
edtTimerValue.setVisibility(View.VISIBLE);
}
}。开始();
}
}
我使用的是下面的code,从时间转换的秒
的格式 HH:MM:SS
的String.Format(%02D:%02D:%02D,durationSeconds / 3600,
(durationSeconds%3600)/ 60,(durationSeconds%60));
您可以轻松地调整该接受输入分
但你的输出将在的格式为HH:MM:00
,因为你没有秒,如:
的String.Format(%02D:%02D:%02D,durationMinutes / 60,durationMinutes 60%,0);
I am trying to develop a countdown timer in android.In this example I am entering a number of minutes as a parameter which is shown in a countdown timer.
The problem is that when I am entering minutes, for example 65,then the counter will be 65:00. I would like the counter to show 1:05:00i.e. in HH:MM:SS format.
public class MainActivity extends Activity implements OnClickListener {
private Button buttonStartTime, buttonStopTime;
private EditText edtTimerValue;
private TextView textViewShowTime; // will show the time
private CountDownTimer countDownTimer; // built in android class
// CountDownTimer
private long totalTimeCountInMilliseconds; // total count down time in
// milliseconds
private long timeBlinkInMilliseconds; // start time of start blinking
private boolean blink; // controls the blinking .. on and off
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttonStartTime = (Button) findViewById(R.id.btnStartTime);
buttonStopTime = (Button) findViewById(R.id.btnStopTime);
textViewShowTime = (TextView) findViewById(R.id.tvTimeCount);
edtTimerValue = (EditText) findViewById(R.id.edtTimerValue);
buttonStartTime.setOnClickListener(this);
buttonStopTime.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.btnStartTime) {
textViewShowTime.setTextAppearance(getApplicationContext(),
R.style.normalText);
setTimer();
buttonStopTime.setVisibility(View.VISIBLE);
buttonStartTime.setVisibility(View.GONE);
edtTimerValue.setVisibility(View.GONE);
edtTimerValue.setText("");
startTimer();
} else if (v.getId() == R.id.btnStopTime) {
countDownTimer.cancel();
buttonStartTime.setVisibility(View.VISIBLE);
buttonStopTime.setVisibility(View.GONE);
edtTimerValue.setVisibility(View.VISIBLE);
}
}
private void setTimer() {
int time = 0;
if (!edtTimerValue.getText().toString().equals("")) {
time = Integer.parseInt(edtTimerValue.getText().toString());
} else
Toast.makeText(MainActivity.this, "Please Enter Minutes...",
Toast.LENGTH_LONG).show();
totalTimeCountInMilliseconds = 60 * time * 1000;
timeBlinkInMilliseconds = 30 * 1000;
}
private void startTimer() {
countDownTimer = new CountDownTimer(totalTimeCountInMilliseconds, 500) {
// 500 means, onTick function will be called at every 500
// milliseconds
@Override
public void onTick(long leftTimeInMilliseconds) {
long seconds = leftTimeInMilliseconds / 1000;
if (leftTimeInMilliseconds < timeBlinkInMilliseconds) {
textViewShowTime.setTextAppearance(getApplicationContext(),
R.style.blinkText);
// change the style of the textview .. giving a red
// alert style
if (blink) {
textViewShowTime.setVisibility(View.VISIBLE);
// if blink is true, textview will be visible
} else {
textViewShowTime.setVisibility(View.INVISIBLE);
}
blink = !blink; // toggle the value of blink
}
textViewShowTime.setText(String.format("%02d", seconds / 60)
+ ":" + String.format("%02d", seconds % 60));
// format the textview to show the easily readable format
}
@Override
public void onFinish() {
// this function will be called when the timecount is finished
textViewShowTime.setText("Time up!");
textViewShowTime.setVisibility(View.VISIBLE);
buttonStartTime.setVisibility(View.VISIBLE);
buttonStopTime.setVisibility(View.GONE);
edtTimerValue.setVisibility(View.VISIBLE);
}
}.start();
}
}
I am using the below code to convert from duration in seconds
to the format HH:MM:SS
.
String.format("%02d:%02d:%02d", durationSeconds / 3600,
(durationSeconds % 3600) / 60, (durationSeconds % 60));
You can easily adjust this to accept input in minutes
but then your output will be in the format of HH:MM:00
since you do not have seconds, e.g.:
String.format("%02d:%02d:%02d", durationMinutes / 60, durationMinutes % 60, 0);
这篇关于倒数计时器在HH:MM:在Android中SS格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!