*仿支付宝输入密码界面(DiaLog)
- 自定义对话框实现仿支付宝交易密码输入功能
private void initDialogContent(final Context context, String moneytxt) {
dialog = new Dialog(context, R.style.TypeDialog);
View view = LayoutInflater.from(context).inflate(R.layout.layout_popup_bottom, null);
valueList = new ArrayList<Map<String, String>>();
tvList = new TextView[6];
setView();//填充键盘数据
imgCancel = (ImageView) view.findViewById(R.id.img_cancel);
tvForget = (TextView) view.findViewById(R.id.tv_forgetPwd);
tvList[0] = (TextView) view.findViewById(R.id.tv_pass1);
tvList[1] = (TextView) view.findViewById(R.id.tv_pass2);
tvList[2] = (TextView) view.findViewById(R.id.tv_pass3);
tvList[3] = (TextView) view.findViewById(R.id.tv_pass4);
tvList[4] = (TextView) view.findViewById(R.id.tv_pass5);
tvList[5] = (TextView) view.findViewById(R.id.tv_pass6);
gridView = (GridView) view.findViewById(R.id.gv_keybord);
adapter=new PayViewAdp(context,valueList);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position < 11 && position != 9) { //点击0~9按钮
if (currentIndex >= -1 && currentIndex < 5) {
//判断输入位置,要小心数组越界
tvList[++currentIndex].setText(valueList.get(position).get("name"));
}
} else {
if (position == 11) { //点击退格键
if (currentIndex - 1 >= -1) { //判断是否删除完毕————要小心数组越界
tvList[currentIndex--].setText("");
}
}
}
}
});
- 重写接口,我这边定义了取消交易接口,和忘记交易密码接口按钮
public interface DialogListener {
/**
* 重写实现Dialog的确定键点击监听
*/
public void okClick();
/**
* 重写实现Dialog消失的时候监听
*/
public void dissmiss();
/**
* 重写实现Dialog的取消键点击监听
*/
public void cancelClick();
}
public void dissmiss(){
dialog.dismiss();
}*
- 3.因为自定义对话框好像无法自动填充全部模版所以需要增加填充代码
Window win = dialog.getWindow();
win.getDecorView().setPadding(0, 0, 0, 0);
WindowManager.LayoutParams lp = win.getAttributes();
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height =WindowManager.LayoutParams.MATCH_PARENT;
win.setAttributes(lp);
dialog.setContentView(view);
dialog.setCanceledOnTouchOutside(false);//设置外部不可点击
- 设置回调方法,以便监听输入完交易密码后的回调
//设置监听方法,在第6位输入完成后触发
public void setOnFinishInput(final OnPasswordInputFinish pass) {
tvList[5].addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if (s.toString().length() == 1) {
strPassword = "";
//每次触发都要先将strPassword置空,再重新获取,避免由于输入删除再输入造成混乱
for (int i = 0; i < 6; i++) {
strPassword += tvList[i].getText().toString().trim();
}
pass.inputFinish();
//接口中要实现的方法,完成密码输入完成后的响应逻辑
}
}
});
}
/* 获取输入的密码 */
public String getStrPassword() {
return strPassword;
}
- setView()方法,填充键盘数据
private void setView() {
/* 初始化按钮上应该显示的数字 */
for (int i = 1; i < 13; i++) {
Map<String, String> map = new HashMap<String, String>();
if (i < 10) {
map.put("name", String.valueOf(i));
} else if (i == 10) {
map.put("name", "");
} else if (i == 11) {
map.put("name", String.valueOf(0));
} else if (i == 12) {
map.put("name", "<<");
}
valueList.add(map);
}
}
- 至于适配器数据,就是一个Textview,这边就不详细写出来了,只需要注意这么一点的逻辑就好;
```
//因为第11Position为删除按钮,需要替换掉
if(position == 9){
holderView.btnKey.setBackgroundResource(R.drawable.selector_key_del);
holderView.btnKey.setEnabled(false);
}
if(position == 11){
holderView.btnKey.setBackgroundResource(R.drawable.selector_key_del);
}
本文参考了他人的逻辑判断,首次发文感谢大家支持;
这边就不上传项目工程,将主要代码上传,有需要的大家可以下载参考一下。
下载链接:http://download.csdn.net/detail/qq_24535769/9591873