forked from zuiwuyuan/WeChatPswKeyboard
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
497 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
pswkeyboardlibrary/src/main/java/com/moziqi/pwd/widget/ZanyEditText.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package com.moziqi.pwd.widget; | ||
|
||
import android.content.Context; | ||
import android.util.AttributeSet; | ||
import android.view.KeyEvent; | ||
import android.view.inputmethod.EditorInfo; | ||
import android.view.inputmethod.InputConnection; | ||
import android.view.inputmethod.InputConnectionWrapper; | ||
import android.widget.EditText; | ||
|
||
/** | ||
* https://blog.csdn.net/yanghuiyu38/article/details/53638601 | ||
*/ | ||
public class ZanyEditText extends EditText { | ||
private OnDelKeyEventListener delKeyEventListener; | ||
|
||
public ZanyEditText(Context context, AttributeSet attrs, int defStyle) { | ||
super(context, attrs, defStyle); | ||
} | ||
|
||
public ZanyEditText(Context context, AttributeSet attrs) { | ||
super(context, attrs); | ||
} | ||
|
||
public ZanyEditText(Context context) { | ||
super(context); | ||
} | ||
|
||
|
||
@Override | ||
public InputConnection onCreateInputConnection(EditorInfo outAttrs) { | ||
|
||
return new ZanyInputConnection(super.onCreateInputConnection(outAttrs), | ||
true); | ||
} | ||
|
||
private class ZanyInputConnection extends InputConnectionWrapper { | ||
|
||
public ZanyInputConnection(InputConnection target, boolean mutable) { | ||
super(target, mutable); | ||
} | ||
|
||
@Override | ||
public boolean sendKeyEvent(KeyEvent event) { | ||
if (event.getAction() == KeyEvent.ACTION_DOWN | ||
&& event.getKeyCode() == KeyEvent.KEYCODE_DEL) { | ||
if (delKeyEventListener != null) { | ||
delKeyEventListener.onDeleteClick(); | ||
return true; | ||
} | ||
} | ||
return super.sendKeyEvent(event); | ||
} | ||
|
||
|
||
@Override | ||
public boolean deleteSurroundingText(int beforeLength, int afterLength) { | ||
if (beforeLength == 1 && afterLength == 0) { | ||
return sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, | ||
KeyEvent.KEYCODE_DEL)) | ||
&& sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, | ||
KeyEvent.KEYCODE_DEL)); | ||
} | ||
|
||
return super.deleteSurroundingText(beforeLength, afterLength); | ||
} | ||
|
||
} | ||
|
||
/** | ||
* 功能描述: <br> | ||
* 〈功能详细描述〉 | ||
* | ||
* @param delKeyEventListener EditText删除回调 | ||
*/ | ||
public void setDelKeyEventListener(OnDelKeyEventListener delKeyEventListener) { | ||
this.delKeyEventListener = delKeyEventListener; | ||
} | ||
|
||
public interface OnDelKeyEventListener { | ||
void onDeleteClick(); | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
...yboardlibrary/src/main/java/com/moziqi/pwd/widget/deledittext/DetectDelEventEditText.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package com.moziqi.pwd.widget.deledittext; | ||
|
||
import android.content.Context; | ||
import android.support.annotation.Nullable; | ||
import android.util.AttributeSet; | ||
import android.view.KeyEvent; | ||
import android.view.View; | ||
import android.view.inputmethod.EditorInfo; | ||
import android.view.inputmethod.InputConnection; | ||
import android.widget.EditText; | ||
|
||
/** | ||
* https://blog.csdn.net/sollian/article/details/60959542 | ||
*/ | ||
public class DetectDelEventEditText extends EditText implements View.OnKeyListener, | ||
EditableInputConnection.OnDelEventListener { | ||
private DelEventListener delEventListener; | ||
|
||
/** | ||
* 防止delEvent触发两次。 | ||
* 0:未初始化;1:使用onKey方法触发;2:使用onDelEvdent方法触发 | ||
*/ | ||
private int flag; | ||
|
||
public DetectDelEventEditText(Context context) { | ||
super(context); | ||
init(); | ||
} | ||
|
||
public DetectDelEventEditText(Context context, | ||
@Nullable AttributeSet attrs) { | ||
super(context, attrs); | ||
init(); | ||
} | ||
|
||
public DetectDelEventEditText(Context context, | ||
@Nullable | ||
AttributeSet attrs, int defStyleAttr) { | ||
super(context, attrs, defStyleAttr); | ||
init(); | ||
} | ||
|
||
private void init() { | ||
setOnKeyListener(this); | ||
} | ||
|
||
@Override | ||
public InputConnection onCreateInputConnection(EditorInfo outAttrs) { | ||
super.onCreateInputConnection(outAttrs); | ||
EditableInputConnection editableInputConnection = new EditableInputConnection(this); | ||
outAttrs.initialSelStart = getSelectionStart(); | ||
outAttrs.initialSelEnd = getSelectionEnd(); | ||
outAttrs.initialCapsMode = editableInputConnection.getCursorCapsMode(getInputType()); | ||
|
||
editableInputConnection.setDelEventListener(this); | ||
flag = 0; | ||
|
||
return editableInputConnection; | ||
} | ||
|
||
public void setDelListener(DelEventListener l) { | ||
delEventListener = l; | ||
} | ||
|
||
@Override | ||
public boolean onKey(View v, int keyCode, KeyEvent event) { | ||
if (flag == 2) { | ||
return false; | ||
} | ||
flag = 1; | ||
return delEventListener != null && keyCode == KeyEvent.KEYCODE_DEL && event | ||
.getAction() == KeyEvent.ACTION_DOWN && delEventListener.delEvent(); | ||
} | ||
|
||
@Override | ||
public boolean onDelEvent() { | ||
if (flag == 1) { | ||
return false; | ||
} | ||
flag = 2; | ||
return delEventListener != null && delEventListener.delEvent(); | ||
} | ||
|
||
// @Override | ||
// public boolean onKey(View v, int keyCode, KeyEvent event) { | ||
// return false; | ||
// } | ||
|
||
public interface DelEventListener { | ||
boolean delEvent(); | ||
} | ||
} |
Oops, something went wrong.