仿微信支付宝密码输入dialog,先上效果图:
dialog使用链式调用设计,提现到账日期为初始化dialog时传入,实现PwdFullListener接口,在输入够六位密码时及获取的密码字符串:
new PayPwdDialog(MainActivity.this, "3").setListener(new PayPwdDialog.PwdFullListener() {
@Override
public void pwdFull(String pwd) {
Toast.makeText(MainActivity.this, pwd, Toast.LENGTH_LONG).show();
}
}).show();
键盘区域为继承LinearLayout的普通控件,在其中实现每个按钮的点击事件,获取到输入的密码,增加一个键盘点击的回调监听,使dialog能获取到键盘的点击事件,点击数字键时将按键代表字符串传出,点击删除键时在dialog中密码字符串删除一位。
/**
* 设置键盘的点击回调监听
*/
public void setOnCustomerKeyboardClickListener(CustomerKeyboardClickListener listener) {
this.mListener = listener;
}
/**
* 点击键盘的回调监听
*/
public interface CustomerKeyboardClickListener {
public void click(String number);
public void delete();
}
密码显示框,主要逻辑为增加一位密码,删除一位密码以及六位密码输入完成
public void setListener(PasswordFullListener listener) {
this.listener = listener;
}
/**
* 增加一位密码
* @param number
*/
public void addPassword(String number) {
number = getText().toString().trim() + number;
if (number.length() > mPasswordNumber) {
return;
}
setText(number);
if (number.length()==mPasswordNumber){
listener.passwordFull(number);
}
}
/**
* 删除最后一位密码
*/
public void deleteLastPassword() {
String currentText = getText().toString().trim();
if (TextUtils.isEmpty(currentText)) {
return;
}
currentText = currentText.substring(0, currentText.length() - 1);
setText(currentText);
}
public interface PasswordFullListener {
void passwordFull(String password);
}
两控件逻辑在dialog中互相调用实现密码输入功能:
@Override
public void click(String number) {
editPwd.addPassword(number);
}
@Override
public void delete() {
editPwd.deleteLastPassword();
}
dialog暴露一个六位密码输入完全的监听事件使activity等实现显示dialog的页面获取密码:
@Override
public void passwordFull(String password) {
listener.pwdFull(password);
dismiss();
}
public interface PwdFullListener {
void pwdFull(String pwd);
}
dialog占满屏宽,重写show方法:
@Override
public void show() {
super.show();
/**
* 设置宽度全屏,要设置在show的后面
*/
if (getWindow() != null) {
WindowManager.LayoutParams params = getWindow().getAttributes();
params.width = WindowManager.LayoutParams.MATCH_PARENT;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
getWindow().setAttributes(params);
getWindow().setGravity(Gravity.BOTTOM);
setCanceledOnTouchOutside(true);
}
}
最后放上demo地址DEMO