Android 8.1 输入框返回键改为删除功能

Android 8.1 输入框返回键改为删除功能

最近接到客户反馈,想要实现在输入框返回键改为删除功能,具体修改参照如下:

/frameworks/base/core/java/android/view/View.java

    public boolean dispatchKeyEvent(KeyEvent event) {
        if (mInputEventConsistencyVerifier != null) {
            mInputEventConsistencyVerifier.onKeyEvent(event, 0);
        }
        if (ViewDebugManager.DEBUG_KEY || ViewDebugManager.DEBUG_ENG) {
            ViewDebugManager.getInstance().debugKeyDispatch(this, event);
        }

        // Give any attached key listener a first crack at the event.
        //noinspection SimplifiableIfStatement
        ListenerInfo li = mListenerInfo;
        if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
                && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
            /// M : add log to help debugging
            if (ViewDebugManager.DEBUG_KEY) {
                ViewDebugManager.getInstance().debugEventHandled(this, event, li.toString());
            }
            return true;
        }

        if (event.dispatch(this, mAttachInfo != null
                ? mAttachInfo.mKeyDispatchState : null, this)) {
            /// M : add log to help debugging
            if (ViewDebugManager.DEBUG_KEY) {
                ViewDebugManager.getInstance().debugEventHandled(this, event, "onKeyXXX");
            }
            return true;
        }

+        View mFocus = getRootView().findFocus();
+        
+        EditText mEdittext;
+	 TextView mTextView;
+        if(mFocus !=null && event.getAction() == KeyEvent.ACTION_DOWN ){
+        if((mFocus instanceof EditText) && !mFocus.toString().contains("IMEI_VALUE")){
+        mEdittext = (EditText) mFocus;
+        if(event.getKeyCode() == KeyEvent.KEYCODE_BACK){
+        if(mEdittext.getText() !=null && mEdittext.getText().length() > 0){
+                        new Thread(new Runnable() {
+                                            public void run() {
+                                            // TODO Auto-generated method stub
+                                           try {
+                                           Instrumentation inst=new Instrumentation();
+                                           inst.sendKeyDownUpSync(KEYCODE_DEL);
+                                           } catch (Exception e) {
+                                           // TODO: handle exception
+                                           }
+                                           }
+                                           }).start();
+                return true;
+                }
+              }
+        }
+        }

重新编译验证,修改生效,输入框返回键已改为删除功能

你可能感兴趣的:(Android,framework,android)