- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的 Android 应用程序中有一个 ListView,它应该包含秒表计时器以及每一行中的开始和暂停按钮。问题是,当我在一行中启动计时器时,如果我在 ListView 中启动属于另一行的另一个计时器,则第二个计时器也会从第一个计时器所在的时间开始。所以基本上他们不是独立的。我如何修改我的代码,以便每一行都有一个独立的计时器,并且它的时间只有在按下它的(开始或停止)按钮时才会生效。
这是我的自定义适配器类
public class CustomAdapterStopWatch extends BaseAdapter {
private final LayoutInflater layoutInflater;
Context context;
public static List<String> timerList;
private long startTime = 0L;
private Handler customHandler;
long timeInMilliseconds = 0L;
long timeSwapBuff = 0L;
long updatedTime = 0L;
public CustomAdapterStopWatch(Context context, List<String> timerList) {
this.context = context;
this.timerList = timerList;
layoutInflater = LayoutInflater.from(context);
customHandler = new Handler();
}
@Override
public int getCount() {
return timerList.size();
}
@Override
public Object getItem(int position) {
return timerList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.stop_watch_list_view, null);
holder = new CustomAdapterStopWatch.ViewHolder();
holder.timerTextView = (TextView) convertView.findViewById(R.id.timerInListView);
holder.startTimer = (Button) convertView.findViewById(R.id.startTimerInListView);
holder.pauseTimer = (Button) convertView.findViewById(R.id.stopTimerInListView);
holder.timerTextView.setTag(position);
holder.startTimer.setTag(position);
holder.pauseTimer.setTag(position);
convertView.setTag(holder);
// Alarm alarm = (Alarm) getItem(position);
} else {
holder = (CustomAdapterStopWatch.ViewHolder) convertView.getTag();
}
holder.timerTextView.setText(timerList.get(position));
holder.startTimer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startTime = SystemClock.uptimeMillis();
customHandler.postDelayed( new Runnable() {
public void run() {
timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
updatedTime = timeSwapBuff + timeInMilliseconds;
int secs = (int) (updatedTime / 1000);
int mins = secs / 60;
secs = secs % 60;
int milliseconds = (int) (updatedTime % 1000);
holder.timerTextView.setText("" + mins + ":"
+ String.format("%02d", secs) + ":"
+ String.format("%03d", milliseconds));
customHandler.postDelayed(this, 0);
}
}, 0);
}
});
holder.pauseTimer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
timeSwapBuff += timeInMilliseconds;
customHandler.removeCallbacks(new Runnable() {
public void run() {
timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
updatedTime = timeSwapBuff + timeInMilliseconds;
int secs = (int) (updatedTime / 1000);
int mins = secs / 60;
secs = secs % 60;
int milliseconds = (int) (updatedTime % 1000);
holder.timerTextView.setText("" + mins + ":"
+ String.format("%02d", secs) + ":"
+ String.format("%03d", milliseconds));
customHandler.postDelayed(this, 0);
}
});
}
});
return convertView;
}
我使用的布局文件如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/timerInListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="00:00:00"
android:textSize="25dp"
android:textColor="@color/colorGray"/>
<Button
android:id="@+id/stopTimerInListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:text="Pause"
android:background="@color/colorGreen"/>
<Button
android:id="@+id/startTimerInListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="22dp"
android:text="Start"
android:background="@color/colorGreen"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/stopTimerInListView"
android:layout_toStartOf="@+id/stopTimerInListView"
android:layout_marginEnd="22dp" />
</RelativeLayout>
我使用适配器的秒表类如下:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stop_watch);
listView = (ListView) findViewById(R.id.listViewInStopWatch);
timerList = new ArrayList<String>();
timerList.add("00:00:000");
timerList.add("00:00:000");
timerList.add("00:00:000");
timerList.add("00:00:000");
customAdapter = new CustomAdapterStopWatch(StopWatch.this, timerList);
listView.setAdapter(customAdapter);
}
更新
我修改了如下代码:
public class CustomAdapterStopWatch extends BaseAdapter {
private final LayoutInflater layoutInflater;
Context context;
public static List<TimerState> timerList;
private long startTime = 0L;
private Handler customHandler;
long timeInMilliseconds = 0L;
long timeSwapBuff = 0L;
long updatedTime = 0L;
HashMap<ViewHolder,Integer> mHashHolder;
TimerState state;
public CustomAdapterStopWatch(Context context, final List<TimerState> timerList) {
this.context = context;
this.timerList = timerList;
layoutInflater = LayoutInflater.from(context);
customHandler = new Handler();
mHashHolder = new HashMap<ViewHolder,Integer>();
state = new TimerState();
// assuming you need to update the timer after every second.
customHandler.postDelayed(new Runnable() {
@Override
public void run() {
for (ViewHolder holder: mHashHolder.keySet()) {
state = timerList.get(mHashHolder.get(holder));
// update the state
}
}
}, 1000);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.stop_watch_list_view, null);
holder = new CustomAdapterStopWatch.ViewHolder();
holder.timerTextView = (TextView) convertView.findViewById(R.id.timerInListView);
holder.startTimer = (Button) convertView.findViewById(R.id.startTimerInListView);
holder.pauseTimer = (Button) convertView.findViewById(R.id.stopTimerInListView);
holder.timerTextView.setTag(position);
holder.startTimer.setTag(position);
holder.pauseTimer.setTag(position);
convertView.setTag(holder);
mHashHolder.put(holder,position);
// Alarm alarm = (Alarm) getItem(position);
} else {
// holder is being used again, so need to update its new position
// following line should be synchronized if timer is updated
// in a separate thread
mHashHolder.put(holder, position);
holder = (CustomAdapterStopWatch.ViewHolder) convertView.getTag();
}
holder.timerTextView.setText(timerList.get(position).currentTime);
holder.startTimer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startTime = SystemClock.uptimeMillis();
if (state.isPaused) {
state.timeSinceStarted = String.valueOf(SystemClock.uptimeMillis());
state.isPaused = false;
} else {
// nothing
}
}
});
holder.pauseTimer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (state.isPaused) {
state.isPaused = true;
}
}
});
return convertView;
}
public void startTime(final TextView t) {
Runnable updateTimerThread = new Runnable() {
public void run() {
timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
updatedTime = timeSwapBuff + timeInMilliseconds;
int secs = (int) (updatedTime / 1000);
int mins = secs / 60;
secs = secs % 60;
int milliseconds = (int) (updatedTime % 1000);
t.setText("" + mins + ":"
+ String.format("%02d", secs) + ":"
+ String.format("%03d", milliseconds));
customHandler.postDelayed(this, 0);
}
};
}
但是在我按下按钮后,没有任何反应。我错过了什么吗?
更新 2
public CustomAdapterStopWatch(Context context, final List<TimerState> timerList) {
this.context = context;
this.timerList = timerList;
layoutInflater = LayoutInflater.from(context);
customHandler = new Handler();
mHashHolder = new HashMap<ViewHolder,Integer>();
state = new TimerState();
// assuming you need to update the timer after every second.
customHandler.postDelayed(new Runnable() {
@Override
public void run() {
for (ViewHolder holder: mHashHolder.keySet()) {
state = timerList.get(mHashHolder.get(holder));
long tmp;
if (!state.isPaused) {
tmp = SystemClock.uptimeMillis();
state.currentTime += tmp - state.timeSinceStarted;
state.timeSinceStarted = tmp;
}
updatedTime = state.currentTime;
int secs = (int) (updatedTime / 1000);
int mins = secs / 60;
secs = secs % 60;
int milliseconds = (int) (updatedTime % 1000);
// set the text of the view in holder
holder.timerTextView.setText("" + mins + ":"
+ String.format("%02d", secs) + ":"
+ String.format("%03d", milliseconds));
// update the state
}
}
}, 1000);
}
更新 3
public class CustomAdapterStopWatch extends BaseAdapter {
private final LayoutInflater layoutInflater;
Context context;
public static List<TimerState> timerList;
private long startTime = 0L;
private Handler customHandler;
long timeInMilliseconds = 0L;
long timeSwapBuff = 0L;
long updatedTime = 0L;
HashMap<ViewHolder,Integer> mHashHolder;
TimerState state;
public CustomAdapterStopWatch(final Context context, final List<TimerState> timerList) {
this.context = context;
this.timerList = timerList;
layoutInflater = LayoutInflater.from(context);
customHandler = new Handler();
mHashHolder = new HashMap<ViewHolder,Integer>();
state = new TimerState();
// assuming you need to update the timer after every second.
customHandler.postDelayed(new Runnable() {
@Override
public void run() {
for (ViewHolder holder: mHashHolder.keySet()) {
state = timerList.get(mHashHolder.get(holder));
long tmp = 0;
if (!state.isPaused) {
tmp = SystemClock.uptimeMillis();
state.currentTime += tmp - state.timeSinceStarted;
state.timeSinceStarted = tmp;
}
updatedTime = state.currentTime;
int secs = (int) (updatedTime / 1000);
int mins = secs / 60;
secs = secs % 60;
int milliseconds = (int) (updatedTime % 1000);
// set the text of the view in holder
holder.timerTextView.setText("" + mins + ":"
+ String.format("%02d", secs) + ":"
+ String.format("%03d", milliseconds));
// notifyDataSetChanged();
Toast.makeText(context,"I got here", Toast.LENGTH_SHORT).show();
// update the state
}
customHandler.postDelayed(this, 1000);
}
}, 1000);
}
@Override
public int getCount() {
return timerList.size();
}
@Override
public Object getItem(int position) {
return timerList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.stop_watch_list_view, null);
holder = new CustomAdapterStopWatch.ViewHolder();
holder.timerTextView = (TextView) convertView.findViewById(R.id.timerInListView);
holder.startTimer = (Button) convertView.findViewById(R.id.startTimerInListView);
holder.pauseTimer = (Button) convertView.findViewById(R.id.stopTimerInListView);
holder.timerTextView.setTag(position);
holder.startTimer.setTag(position);
holder.pauseTimer.setTag(position);
convertView.setTag(holder);
mHashHolder.put(holder,position);
// Alarm alarm = (Alarm) getItem(position);
} else {
// holder is being used again, so need to update its new position
// following line should be synchronized if timer is updated
// in a separate thread
mHashHolder.put(holder, position);
holder = (CustomAdapterStopWatch.ViewHolder) convertView.getTag();
}
holder.timerTextView.setText(String.valueOf(timerList.get(position)));
holder.startTimer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startTime = SystemClock.uptimeMillis();
if (state.isPaused) {
state.timeSinceStarted = SystemClock.uptimeMillis();
state.isPaused = false;
} else {
// nothing
}
}
});
holder.pauseTimer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!state.isPaused) {
state.isPaused = true;
}
}
});
return convertView;
}
public void startTime(final TextView t) {
Runnable updateTimerThread = new Runnable() {
public void run() {
timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
updatedTime = timeSwapBuff + timeInMilliseconds;
int secs = (int) (updatedTime / 1000);
int mins = secs / 60;
secs = secs % 60;
int milliseconds = (int) (updatedTime % 1000);
t.setText("" + mins + ":"
+ String.format("%02d", secs) + ":"
+ String.format("%03d", milliseconds));
customHandler.postDelayed(this, 0);
}
};
}
private class ViewHolder {
TextView timerTextView;
Button startTimer;
Button pauseTimer;
}
}
最佳答案
实际上你做错了两件事。
您为每个计时器使用相同的变量 updateTime。相反,您应该为每个计时器维护一个状态。所以最好为此开设一个单独的类(class)。例如
class TimerState { boolean isPaused = true; long currentTime = 0; String timeSinceStarted; // matters only when the timer is started }
So pass the array list of above class to your adapter. So that timerList will be arrayList of TimerState.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stop_watch);
listView = (ListView) findViewById(R.id.listViewInStopWatch);
timerList = new ArrayList<TimerState>();
timerList.add(new TimerState());
timerList.add(new TimerState());
timerList.add(new TimerState());
timerList.add(new TimerState());
customAdapter = new CustomAdapterStopWatch(StopWatch.this, timerList);
listView.setAdapter(customAdapter);
}
你的适配器将是
public class CustomAdapterStopWatch extends BaseAdapter { private final LayoutInflater layoutInflater; Context context; public static List timerList; private long startTime = 0L; private Handler customHandler; long timeInMilliseconds = 0L; long timeSwapBuff = 0L; long updatedTime = 0L; // this will contain the holder of all visible views HashMap mHashHolder; public CustomAdapterStopWatch(Context context, List timerList) { this.context = context; this.timerList = timerList; layoutInflater = LayoutInflater.from(context); customHandler = new Handler(); // assuming you need to update the timer after every second. customHandler.postDelayed(new Runnable() { @Override public void run() { TimerState state; for (ViewHolderItem holder: mHashHolder.keySet()) { state = timerList.get(mHashHolder.get(holder)); long tmp; if (!state.isPaused) { tmp = SystemClock.uptimeMillis(); state.currentTime += tmp - state.timeSinceStarted) state.timeSinceStarted = tmp; } updatedTime = state.currentTime int secs = (int) (updatedTime / 1000); int mins = secs / 60; secs = secs % 60; int milliseconds = (int) (updatedTime % 1000); // set the text of the view in holder holder.t.setText("" + mins + ":" + String.format("%02d", secs) + ":" + String.format("%03d", milliseconds)); } customHandler.postDelayed(this, 1000); } }, 1000); } @Override public int getCount() { return timerList.size(); } @Override public Object getItem(int position) { return timerList.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { final ViewHolder holder; if (convertView == null) { convertView = layoutInflater.inflate(R.layout.stop_watch_list_view, null); holder = new CustomAdapterStopWatch.ViewHolder(); holder.timerTextView = (TextView) convertView.findViewById(R.id.timerInListView); holder.startTimer = (Button) convertView.findViewById(R.id.startTimerInListView); holder.pauseTimer = (Button) convertView.findViewById(R.id.stopTimerInListView); holder.timerTextView.setTag(position); holder.startTimer.setTag(position); holder.pauseTimer.setTag(position); convertView.setTag(holder); mHashHolder.put(position, holder); // Alarm alarm = (Alarm) getItem(position); } else { // holder is being used again, so need to update its new position // following line should be synchronized if timer is updated // in a separate thread holder = (CustomAdapterStopWatch.ViewHolder) convertView.getTag(); mHashHolder.put(holder, position) } holder.timerTextView.setText(timerList.get(position)); holder.startTimer.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startTime = SystemClock.uptimeMillis(); if (state.isPaused) { state.timeSinceStarted = SystemClock.uptimeMillis(); state.isPaused = false; } else { // nothing } // logic for showing the timer will be in onScroll method } }); holder.pauseTimer.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (!state.isPaused) { state.isPaused = True } } }); return convertView; }
关于android - ListView 中的多个秒表计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40283183/
我需要在 文档就绪 触发后 5 秒调用我的函数 abc()。 这在 jQuery 中可能吗? $(document).ready(function () { //Wait 5 seconds then
我发现 System.Diagnostics.Stopwatch 类即使在很短的时间段(即 20 秒)内似乎也存在可测量的不准确性。对于编码为运行 20 秒的进程,我的进程显示耗时为 20.3+ 秒:
我正在使用 Ionic Framework 使用 Angular 构建一个 Android/iPhone 食谱应用程序。 功能之一应该是每个成分/步骤的警报/计时器。 我猜 Ionic 是基于 Apa
我有一个 JS 计时器脚本,从 20 秒开始倒计时 var count = 0; var speed = 1000; countdown = setInterval( function
我的 JavaScript 计时器有一个大问题。一切正常,除了 1 个按钮:停止!我不知道如何停止计数器上的所有内容(例如重置每个输入和计数器)。有什么想法可以解决这个问题吗?
所以我有一个 TimerTask 任务 在其 run() 中调用函数 onTimerComplete() onTimerComplete() 看起来像这样: private void onTimerC
我在 Eclipse 中的 windowbuilder 创建的 run() 方法中有计时器。 我不知道如何通过另一种方法停止该计时器。例如通过添加 if(MenuWindow.gameStarted
我对java很陌生,我试图在按下按钮时启动计时器,但无法启动它。我尝试了几行代码,但似乎没有任何效果。 我已经声明了我的标签、文本字段和计时器: private JTextField JTFHours
import java.util.Scanner; import java.util.Timer; import java.util.TimerTask; public class Boggle {
我正在尝试在 JavaScript 中获取计时器的值,因此当计时器为零时它会显示不同的内容; var local_t = new Date(); local_t.setSeconds(local_t.
我需要使用 jquery 为网站创建自定义 slider 。到目前为止,如果我单击按钮,我已经设法让它按照我想要的方式运行,但我想实现一个自动计时器,以便幻灯片在 5 秒后切换。 这是我的 JS 代码
我正在制作一个计时器,记录点击“通过”按钮上的“确定”时的最长时间(个人最好成绩)。我似乎无法弄清楚如何让计数器回到 0,我确信有一种方法可以只用 2 个按钮(开始 + 通过)来完成。我希望计时器在我
我有一个 java.util.Timer 用于限制电子邮件发送(如果最近发送了状态电子邮件,那么现在不要发送,而是创建一个计时器任务以稍后发送任何新状态)。 我想要的是确保最终发送所有排队的电子邮件,
我已经建立了一个在线考试门户,允许学生使用网络浏览器在线参加考试。 现在我还开发了计时器功能,用户必须在规定的时间范围内完成考试。我遇到的麻烦是,当我刷新页面时,计时器再次从新开始,例如 40 分钟。
我想知道在一定时间内禁用按钮的最佳方法是什么。当用户使用其他按钮转到其他页面时,定时器不会被重置,按钮仍将被禁用。 我读过很多关于异步任务、线程的不同方法。但是我非常不确定哪种方法最好。 我希望计时器
我正在制作一个测验应用程序,我想在其中显示用户在玩游戏时所用的时间。它应该采用 HH:MM:SS 格式,从 00:00:00 开始,直到他选择答案。当用户每秒播放时,计时器应该每秒更新一次。另外,我想
您好,计划为该 Activity 开发 android 倒数计时器应用程序,当用户单击开始按钮时显示计时器倒计时并显示计时器倒计时,用户将转到剩余 Activity ,即使计时器在后台运行,如果用户单
我想知道是否有一种简单的方法可以使用 PHP 在数据库中创建计时器,我想在数据库中创建该行 30 分钟后将其删除。如果这不够具体,我很抱歉,但我不知道如何在其中提供更多细节,请随意发表评论,以便我可以
我试图制作一种与我的计时器一起使用的对象。问题是,当我只有裸函数(不在对象中)时,它就可以工作。但是当我把它放在对象内部时它不起作用。 使用此代码我只能看到 00:01 当我只使用函数本身时,它工作正
这个问题已经有答案了: How to format numbers as currency strings (67 个回答) 已关闭 9 年前。 我想显示从 1 到 2500 的美元计时器。当计时器增
我是一名优秀的程序员,十分优秀!