我目前正在使用可运行的处理程序和一个计时器,该计时器每秒更新一次文本视图。
public class TimerFragment extends Fragment {
TextView tvTimer;
long startTime = 0;
Handler timerHandler = new Handler();
Runnable timerRunnable = new Runnable() {
@Override
public void run() {
long millis = System.currentTimeMillis() - startTime;
int seconds = (int)(millis/1000);
int minutes = seconds / 60;
seconds = seconds % 60;
tvTimer.setText(String.format("%d:%02d", minutes, seconds));
timerHandler.postDelayed(this,500);
}
};
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag_timer, null);
tvTimer = (TextView)view.findViewById(R.id.tvTimer);
Button btn = (Button)view.findViewById(R.id.btn1);
btn.setText("Start");
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Button b = (Button) v;
if (b.getText().equals("Stop")){
timerHandler.removeCallbacks(timerRunnable);
b.setText("Start Monitoring");
}
else {
startTime = System.currentTimeMillis();
timerHandler.postDelayed(timerRunnable, 0);
b.setText("Stop");
Toast.makeText(getActivity(),"Monitoring started.",Toast.LENGTH_SHORT).show();
}
}
});
return view;
}
}
如何获取计时器的值?例如,当文本变为“ 0:15”时显示敬酒。我希望能够获得该值,以便可以使用它。我想到的功能是让用户设置时间,并在计时器经过该时间(例如15或30秒)时弹出祝酒词或通知。但是现在我想知道的是如何获取计时器的值。谢谢!
答案 0 :(得分:0)
您应该使用CountDownTimer来根据您设置的间隔设置倒计时,并且在未来时间到来时将停止
protected void Page_Load(object sender, EventArgs e)
{
_searchDT = _dtMgr.GetTicketsByKeyword(uxKeywordTextBox.Text, null);
....
if (!Page.IsPostBack)
{
DataRow dr = null;
dr = _searchDT.NewRow();
_searchDT.Rows.Add();
uxSearchGridView.DataSource = _searchDT;
uxSearchGridView.DataBind();
}
}
...
}
For more insights refer the Android Developer reference for CountDownTimer