Chronometer计时器:
布局引用这个控件即可
ch.setBase(SystemClock.elapsedRealtime());
ch.setFormat("%S");
ch.start();
ch.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
@Override
public void onChronometerTick(Chronometer chronometer) {
if(SystemClock.elapsedRealtime()-ch.getBase()>=60000){
ch.stop();
}
}
});
日期格式化:
// 格式化 时:分:秒 自己想要哪部分截取哪部分
public static String FormatMiss(int miss) {
String hh = miss / 3600 > 9 ? miss / 3600 + "" : "0" + miss / 3600;
String mm = (miss % 3600) / 60 > 9 ? (miss % 3600) / 60 + "" : "0" + (miss % 3600) / 60;
String ss = (miss % 3600) % 60 > 9 ? (miss % 3600) % 60 + "" : "0" + (miss % 3600) % 60;
return hh + ":" + mm + ":" + ss;
}
实现录音的计时:
类似于倒计时(倒计时是减减,这个用加加就可以计时了),最后把日期格式化一下,即可,我这里不同数字要在不同框,所以我进行了截取
mMusicStart.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
startRecord();
mTimer = new Timer();
mTimer.schedule(new TimerTask() {
@Override
public void run() {
mContext.runOnUiThread(new Runnable() {
@Override
public void run() {
time++;
String s = FormatMiss(time);
String substring = s.substring(0, 1);
String substring1 = s.substring(1, 2);
String substring2 = s.substring(3, 4);
String substring3 = s.substring(4, 5);
one.setText(substring);
two.setText(substring1);
three.setText(substring2);
four.setText(substring3);
Log.i("凉城时间", substring+"\n"+substring1+"\n"+substring2+"\n"+substring3);
}
});
}
}, 0, 1000);
Toast.makeText(mContext, "按住录音中", Toast.LENGTH_SHORT).show();
}
if (event.getAction() == MotionEvent.ACTION_UP) {
stopRecord();
mTimer.cancel();
Toast.makeText(mContext, "松开结束录音", Toast.LENGTH_SHORT).show();
}
return true;
}
});