Android 强行夺取EditText系统键盘输入改为自定义安全键盘输入

 很多时候Android开发人员会遇到公司的产品让在输入密码的时候不能调用系统键盘,需要自己研发自定义键盘,效果还要跟调用系统键盘一样,很多开发人员需要自己写一套自定义键盘 ,但是很多人卡在了EditText输入时怎么配合自定义键盘,还要光标的配合 ,废话不多说直接上代码在底部也有我自己的代码,仅供下载。 http://download.****.net/detail/qq965401697/9880725


Android 强行夺取EditText系统键盘输入改为自定义安全键盘输入



pwdEdit.setOnClickListener(this);

pwdEdit.setOnFocusChangeListener(this);
pwdEdit.setOnTouchListener(new View.OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {

int inType = pwdEdit.getInputType();
pwdEdit.setInputType(InputType.TYPE_NULL);
pwdEdit.onTouchEvent(event);
pwdEdit.setInputType(inType);

return true;
}

});




/**
* 创建PopupWindow
*/
private void initPopupWindow() {
View popupWindow_view = getLayoutInflater().inflate(R.layout.popup_loginkey_layout2, null, false);
popupWindow = new PopupWindow(popupWindow_view, ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT, true);
popupWindow.setFocusable(false);
popupWindow.setTouchable(true);
popupWindow.setOutsideTouchable(true);
popupWindow.setAnimationStyle(R.style.AnimationFade);

//点击按钮以外的其它地方,键盘自动隐藏
popupView.setOnTouchListener(new View.OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {

hidePopupWindow();

return false;
}
});
}

/**隐藏PopupWindow*/
private void hidePopupWindow(){
if(popupWindow != null && popupWindow.isShowing()){
popupWindow.dismiss();
popupWindow = null;
}

isPopupKeyWindow = false;
}


/*设置EditText*/
private void setEdittext(EditText et, String key, String allKey) {

cursorIndex = et.getSelectionStart();
et.setText(allKey);
if (!"cancel".equals(key)) {
cursorIndex++;
} else {

if (cursorIndex > 0) {
cursorIndex--;
}
}
//将光标的位置跳到index处
Editable editable = et.getEditableText();
Selection.setSelection(editable, cursorIndex);
}

@Override
public void onClick(View v) {

switch (v.getId()) {
case R.id.inputpwd_confirm:
checkFrom();
break;
case R.id.inputpwd_pwd:
getPopupWindow();
popupWindow.showAtLocation((View) v.getParent(), Gravity.BOTTOM, 0, 40);

break;
default:
break;
}

}

@Override
public void onFocusChange(View v, boolean hasFocus) {


if(v.getId() == R.id.inputpwd_pwd){
if(pwdEdit.hasFocus()){
popupView.resetListKeys(pwdEdit.getText().toString());
getPopupWindow();
popupWindow.showAtLocation((View) v.getParent(), Gravity.BOTTOM, 0, 40);
}
}
}

/**得到PopupWindow*/
private void getPopupWindow(){

if(null != popupWindow){
// popupWindow.dismiss(); //注释掉这句,按 回车键 时不让数字键盘跳动
return;
}else{
initPopupWindow();

}
isPopupKeyWindow = true;
}


class MySoftKeys implements ISoftKeys {

public void getKeyValues(String key, String allKey, String letterKey) {
//光标的处理


if (pwdEdit.isFocused()) {
setEdittext(pwdEdit, key, allKey);
}
}

// public void setLoginKey() {
// Toast.makeText(getApplicationContext(), "login...", Toast.LENGTH_SHORT).show();
// }

public void setReturnKey() {
hidePopupWindow();
}

public int getCursorIndex() {

if (pwdEdit.isFocused()) {
cursorIndex = pwdEdit.getSelectionStart();
}
System.out.println("cursorIndex:" + cursorIndex);
return cursorIndex;
}

public String getText() {
if (pwdEdit.isFocused()) {
return pwdEdit.getText().toString();
}
return null;
}
}