我在Android游戏中相当新,我试图建立一个类似于android计时器的CountDown计时器。
到目前为止,这是我的代码,后面是我的一些问题
public class TimeTextWatcher implements TextWatcher {
private final int TEXT_HOUR = 0;
private final int TEXT_MINUTE = 1;
private final int TEXT_SECOND = 2;
private EditText Hours;
private EditText Minutes;
private EditText Seconds;
private int Type;
public TimeTextWatcher(EditText h, EditText m, EditText s, int type){
Hours = h;
Minutes = m;
Seconds = s;
Type = type;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Hours.removeTextChangedListener(this);
Hours.setText(String.format("%02d", Integer.parseInt(s.toString())));
Hours.setSelection(Hours.getText().length());
Hours.addTextChangedListener(this);
}
@Override
public void afterTextChanged(Editable s) {
int curr_value;
if (s.toString().equals(""))
curr_value = -1;
else
curr_value = Integer.parseInt(s.toString());
switch(Type){
case TEXT_HOUR:
if (curr_value > 9)
Minutes.requestFocus();
break;
case TEXT_MINUTE:
if (curr_value > 59)
s.replace(0, 2, Integer.toString(59));
if (curr_value > 9)
Seconds.requestFocus();
break;
case TEXT_SECOND:
if (curr_value > 59)
s.replace(0, 2, Integer.toString(59));
break;
default:
Log.d("TimeTextWatcher", "Error in switch case");
}
}
}
正如我所说,我试图让它的行为类似于CountDown计时器内置的android。 一些信息:
addTextChangedListener
。我的目标:
我已尝试使用new DecimalFormat("00")
或setText(String.format("%02d", Integer.parseInt(s.toString())))
,但在按下一位数后会卡住。
希望我很清楚.. 谢谢!