- 由于用对话框做输入支付密码会出现宽度无法充满,这次我用popwindow来做,功能效果更加容易扩展;这次提供源码噢,有兴趣的可以下载去参考下,带动画噢;
- 先看看效果图把;

- 主界面只有这么简单,就不多在去介绍了;
button=(Button)findViewById(R.id.home_button);
background=(View)findViewById(R.id.home_background);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
popWindow = new PayPopWindow(context,background);
popWindow.showAsDropDown(background);
popWindow.setOnFinishInput(new OnPasswordInputFinish() {
@Override
public void inputFinish() {
Toast.makeText(context,popWindow.getStrPassword(), 1).show();
popWindow.StartAnima();
}
});
}
});
- 主要的功能逻辑是写在popwindow里面,介绍大家看看注释就好
public PayPopWindow(final Context context, View backgroundView) {
this.context=context;
this.backgroundView=backgroundView;
View view=LayoutInflater.from(context)
.inflate(R.layout.layout_popup_bottom, null);
popupWindow=new PopupWindow
(view, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
popupWindow.setAnimationStyle(R.style.popWindow_anim_style);
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
popupWindow.setOnDismissListener(this);
valueList = new ArrayList<Map<String, String>>();
tvList = new TextView[6];
setView();
imgCancel = (ImageView) view.findViewById(R.id.img_cance);
tvForget = (TextView) view.findViewById(R.id.tv_forgetPwd);
line=(LinearLayout)view.findViewById(R.id.pay_lin);
pic=(ImageView)view.findViewById(R.id.pay_status);
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() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if (position < 11 && position != 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("");
}
}
}
}
});
imgCancel.setOnClickListener(this);
tvForget.setOnClickListener(this);
}
public interface OnItemClickListener{
/** 设置点击确认按钮时监听接口 */
public void onClickOKPop();
}
/**设置监听*/
public void setOnItemClickListener(OnItemClickListener listener){
this.listener=listener;
}
@Override
public void onDismiss() {
setBackgroundBlack(backgroundView, 1);
popupWindow.dismiss();
}
/**弹窗显示的位置*/
public void showAsDropDown(View position){
popupWindow.showAtLocation(position, Gravity.BOTTOM, 0, 0);
popupWindow.setFocusable(true);
popupWindow.setOutsideTouchable(true);
popupWindow.update();
setBackgroundBlack(backgroundView, 0);
}
/** 控制背景变暗 0变暗 1变亮 */
private void setBackgroundBlack(View view, int what) {
switch (what) {
case 0:
view.setVisibility(View.VISIBLE);
break;
case 1:
view.setVisibility(View.GONE);
break;
}
}
public void StartAnima(){
line.setVisibility(View.VISIBLE);
gridView.setVisibility(View.GONE);
animationDrawable = (AnimationDrawable) pic.getDrawable();
animationDrawable.start();
int duration = 0;
for(int i=0;i<animationDrawable.getNumberOfFrames();i++){
duration += animationDrawable.getDuration(i);
}
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
onDismiss();
}
}, duration);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.img_cance:
onDismiss();
break;
case R.id.tv_forgetPwd:
Toast.makeText(context, "前往忘记密码界面", 1).show();
break;
default:
break;
}
}
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 = "";
for (int i = 0; i < 6; i++) {
strPassword += tvList[i].getText().toString().trim();
}
pass.inputFinish();
}
}
});
}
public String getStrPassword() {
return strPassword;
}
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);
}
}