We often use the mobile phone call function. When we press the keyboard, there is a place where the content of our buttons is displayed. When our hands click that place, the software disk is not displayed, so when we have a digital keyboard, We need to block the system's software disk. Let's analyze the conditions for the software disk to pop up: 1. Focus. When EditText is in focus, the software disk will pop up automatically, so we need to rewrite the onFocusChanged function 2. Touch time, when you click EditText, it will be in the focus, so we need to rewrite the onTouchEvent function 3. When the layout changes, EditText will also be in the focus, so we should also rewrite the layout function.
Package com. jwzhangjie. pjsip. ui. dialpad; import android. content. context; import android. graphics. rect; import android. text. inputType; import android. util. attributeSet; import android. view. motionEvent; import android. view. accessibility. accessibilityEvent; import android. view. inputmethod. inputMethodManager; import android. widget. editText;/*** number input. letter input is not supported at the moment, so the keyboard is completely shielded ** @ author ~zhangjie */publi C class DigitsEditText extends EditText {public DigitsEditText (Context context) {super (context); init () ;}public DigitsEditText (Context context, AttributeSet attrs) {super (context, attrs ); init ();} public DigitsEditText (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle); init ();} private void init () {// set a line to display this. setInputType (InputType. TYPE_NULL) ;}@ Overr Ide public boolean onTouchEvent (MotionEvent event) {final boolean ret = super. onTouchEvent (event); // Must be done after super. onTouchEvent () applyKeyboardShowHide (); return ret ;}@ Override public void sendAccessibilityEventUnchecked (AccessibilityEvent event) {if (event. getEventType () = AccessibilityEvent. TYPE_VIEW_TEXT_CHANGED) {// Since we're re replacing the text every time we add or remove // Character, only read the difference. (issue 5337550) final int added = event. getAddedCount (); final int removed = event. getRemovedCount (); final int length = event. getBeforeText (). length (); if (added> removed) {event. setRemovedCount (0); event. setAddedCount (1); event. setFromIndex (length);} else if (removed> added) {event. setRemovedCount (1); event. setAddedCount (0); event. setFromIndex (length -1);} else {return;} else if (event. getEventType () = AccessibilityEvent. TYPE_VIEW_FOCUSED) {// The parent EditText class lets tts read "edit box" when this View // has a focus, which // confuses users on app launch (issue 5275935 ). return;} super. sendAccessibilityEventUnchecked (event);} @ Override protected void onLayout (boolean changed, int left, int top, int right, int bottom) {super. OnLayout (changed, left, top, right, bottom ); // Here we ensure that we hide the keyboard // Since this will be fired when virtual keyboard this will probably // blink but for now no better way were found to hide keyboard for sure applyKeyboardShowHide ();} @ Override protected void onFocusChanged (boolean focused, int direction, Rect previuslyfocusedrect) {super. onFocusChanged (focused, direction, Response); if (focused) {applyKeyboardShowHide ();} else {final InputMethodManager imm = (InputMethodManager) getContext (). getSystemService (Context. INPUT_METHOD_SERVICE); if (imm! = Null & imm. isActive (this) {imm. hideSoftInputFromWindow (getApplicationWindowToken (), 0) ;}} private void applyKeyboardShowHide () {final InputMethodManager imm = (InputMethodManager) getContext (). getSystemService (Context. INPUT_METHOD_SERVICE); if (imm! = Null) {if (imm. isActive (this) {imm. hideSoftInputFromWindow (getApplicationWindowToken (), 0 );}}}}