我是android studio的新手,正在创建一个移动应用程序。我的目标是用户可以单击开始/暂停按钮,每个按钮单击的当前时间记录在文本视图中。 我正在使用的代码 - 在暂停按钮单击时,它获取当前时间但是在开始按钮上单击它只是与暂停当前时间无效同时获取。我还希望在每次点击按钮时更新当前时间,但它似乎不起作用
pause = myInflatedView.findViewById(R.id.buttonpause);
pause.setBackgroundResource(R.drawable.pause);
showdate = myInflatedView.findViewById(R.id.textViewDate);
showdate2 = myInflatedView.findViewById(R.id.textViewDate2);
calendar = Calendar.getInstance();
calendar2 = Calendar.getInstance();
simpleDateFormat = new SimpleDateFormat("dd mm yyyy HH:mm:ss");
Date = simpleDateFormat.format(calendar.getTime());
Date2 = simpleDateFormat.format(calendar2.getTime());
pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (isPressed) {
view.setBackgroundResource(R.drawable.pause);
Date2 = simpleDateFormat.format(calendar.getTime());
showdate2.setText(simpleDateFormat.format(calendar.getTime()));
}else {
view.setBackgroundResource(R.drawable.start);
Date = simpleDateFormat.format(calendar.getTime());
showdate.setText(simpleDateFormat.format(calendar.getTime()));
}
isPressed = !isPressed;
}
});
有人可以帮我解决我的问题吗?
非常感谢所有帮助。
答案 0 :(得分:1)
单击时,应该执行此操作
pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (isPressed) {
view.setBackgroundResource(R.drawable.pause);
//do this
showdate2.setText(getNowDatess("yyyy-MM-dd HH:mm:ss"));
}else {
view.setBackgroundResource(R.drawable.start);
//do this
showdate.setText(getNowDatess("yyyy-MM-dd HH:mm:ss"));
}
isPressed = !isPressed;
}
});
/**
* get the now time
* @param formart yyyy-MM-dd HH:mm:ss or other
*/
public static String getNowDatess(String formart) {
try {
return getFormatDate(System.currentTimeMillis(), formart);
} catch (Exception e) {
return null;
}
}
public static String getFormatDate(long date, String formatString) {
String formatData;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatString);
formatData = simpleDateFormat.format(date);
return formatData;
}
答案 1 :(得分:0)
然后试试这个:
....
pause = myInflatedView.findViewById(R.id.buttonpause);
pause.setBackgroundResource(R.drawable.pause);
showdate = myInflatedView.findViewById(R.id.textViewDate);
showdate2 = myInflatedView.findViewById(R.id.textViewDate2);
// Get Rid of All of this!!
//calendar = Calendar.getInstance();
//calendar2 = Calendar.getInstance();
//simpleDateFormat = new SimpleDateFormat("dd mm yyyy HH:mm:ss");
//Date = simpleDateFormat.format(calendar.getTime());
//Date2 = simpleDateFormat.format(calendar2.getTime());
pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (isPressed) {
view.setBackgroundResource(R.drawable.pause);
String now = getDateNow();
long nowLong = getMillisecondsNow();
Log.e("My Time Milli 1 = ", "" + nowLong )
//Date2 = simpleDateFormat.format(calendar.getTime());
showdate2.setText(now);
}else {
view.setBackgroundResource(R.drawable.start);
String now = getDateNow();
long nowLong = getMillisecondsNow();
Log.e("My Time Milli 2 = ", "" + nowLong )
//Date = simpleDateFormat.format(calendar.getTime());
showdate.setText(now);
}
isPressed = !isPressed;
}
});
private String getDateNow() {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(""dd mm yyyy HH:mm:ss"");
return sdf.format(cal.getTime());
}
此方法仅用于测试日历。不是为了实际的时间测量目的!!
private long getMillisecondsNow(){
Calendar cal = Calendar.getInstance();
return cal.getTimeInMillis();
}
免责声明:这是一个基于您的问题参数的解决方案。如果你想要一个时间测量应用程序,那么我不推荐这个解决方案。要使用SystemClock.elapsedRealtime();