我试图在屏幕上显示一个计时器,在屏幕上我有一个TextView和一个RecyclerView
RecyclerView包含一些Spinners
执行onTick()
方法时,Spinners会自动关闭。
请提供此问题的任何解决方案。
我的CountDownTimer代码
public class CountDownTimerClass extends CountDownTimer {
public CountDownTimerClass(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onTick(long millisUntilFinished) {
String time = String.format(Locale.US, "%02d:%02d:%02d",
TimeUnit.MILLISECONDS.toHours(millisUntilFinished),
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) -
TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millisUntilFinished)),
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)));
mTvTimer.setText(time);
}
@Override
public void onFinish() {
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_member_enter_details);
mRecyclerView = (RecyclerView) findViewById(R.id.recycleView);
mTvTimer = (TextView) findViewById(R.id.tv_timer);
mTvBlockNo = (TextView) findViewById(R.id.tv_block_no);
mRlHeader = findViewById(R.id.rlHeader);
//hit for web service. In the response i am starting timer
getData(Event.GET_EXPIRATION_TIME);
}
答案 0 :(得分:0)
非常确定它正在关闭,因为你在另一个视图上调用了setText
。这可能会触发焦点更改,从而导致Spinner
关闭。
您可以通过在TextView上将focusable
设置为false(最好是通过XML),或者在clearFocus()
setText()
来避免这种情况
答案 1 :(得分:0)
您应该更新UI线程中的标签或textview文本,因此您的代码应该是
public void onTick(long millisUntilFinished) {
String time = String.format(Locale.US, "%02d:%02d:%02d",
TimeUnit.MILLISECONDS.toHours(millisUntilFinished),
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) -
TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millisUntilFinished)),
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)));
runOnUiThread(new Runnable() {
@Override
public void run() {
mTvTimer.setText(time);
}
});
}