1.首先需要明确接口,确定找回密码流程以及各种存在的用例
2.DigitsKeyListener
通过java代码来对TextView设置KeyListener
KeyListener是一个接口,提供了对输入键盘按键的监听
InputFilter是一个接口,提供了对字符的过滤
android提供了实现了KeyListener和InputFilter的NumberKeyListener,而DigitsKeyListener继承了NumberKeyListener
如果想要实现更大自由度的过滤定制,可以自己写一个KeyListener(继承BaseKeyListener)并实现InputFilter,重写filter()函数,在filter()函数里可以实现自由的过滤。
TextView tv = new TextView(context);
//只接受整数输入
KeyListener l = new DigitsKeyListener(fasle,false);
//接受有符号整数输入
KeyListener l = new DigitsKeyListener(true,false);
//接受小数,整数输入
KeyListener l = new DigitsKeyListener(false,true);
//接受有符号整数/小数输入
KeyListener l = new DigitsKeyListener(true,true);
tv.setKeyListener(l);
3.监听EditText输入框变化,实现TextWatcher接口,重写以三个方法 即可做相关监听和操作.
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
code=edtCode.getText().toString().trim();
if(code.length()==6){
btnNext.setBackgroundResource(R.drawable.btnselector);
}else{
btnNext.setBackgroundResource(R.drawable.sso_login_transparent);
}
}
4.获取短信验证码按钮60s倒计时实现:
法1:通过TextView加CountDownTimer实现
//显示60s倒计时按钮
CountDownTimer timer = new CountDownTimer(60 * 1000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
SpannableStringBuilder builder = new SpannableStringBuilder("" + millisUntilFinished / 1000 + "s后可重发");
builder.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.greenBg)), 0, builder.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
builder.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.edittext_hint_color)), builder.length() - 4, builder.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tvGetCode.setText(builder);
}
@Override
public void onFinish() {
tvGetCode.setEnabled(true);
tvGetCode.setText(R.string.regUsrtips_get_phone_code);
}
};
法2:通过Button和继承CountDownTimer实现
public class TimerCount extends CountDownTimer {
private Button bnt;
public TimerCount(long millisInFuture, long countDownInterval, Button bnt) {
super(millisInFuture, countDownInterval);
this.bnt = bnt;
}
public TimerCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
// TODO Auto-generated constructor stub
}
@Override
public void onFinish() {
// TODO Auto-generated method stub
bnt.setClickable(true);
bnt.setText("获取验证码");
}
@Override
public void onTick(long arg0) {
// TODO Auto-generated method stub
bnt.setClickable(false);
bnt.setText(arg0 / 1000 + "秒后重新获取");
}
}
本质上都是基于CountDownTimer实现的,重写其相关方法实现.
5.关于json解析,可以在捕获的异常里去做一些操作,当没有对应标签时会抛异常.