Home | History | Annotate | Download | only in loopback
      1 /*
      2  * Copyright (C) 2016 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package org.drrickorang.loopback;
     18 
     19 import android.content.Context;
     20 import android.util.AttributeSet;
     21 import android.view.KeyEvent;
     22 import android.view.inputmethod.EditorInfo;
     23 import android.widget.EditText;
     24 import android.widget.TextView;
     25 
     26 /**
     27  *  Provides a callback for both soft-keyboard dismissed or confirm submission button
     28  */
     29 public class CatchEventsEditText extends EditText implements TextView.OnEditorActionListener {
     30 
     31     public interface EditTextEventListener {
     32         public void textEdited(EditText v);
     33     }
     34 
     35     private EditTextEventListener mEditListener;
     36 
     37     public CatchEventsEditText(Context context) {
     38         super(context);
     39         setOnEditorActionListener(this);
     40     }
     41 
     42     public CatchEventsEditText(Context context, AttributeSet attrs) {
     43         super(context, attrs);
     44         setOnEditorActionListener(this);
     45     }
     46 
     47     public CatchEventsEditText(Context context, AttributeSet attrs, int defStyleAttr) {
     48         super(context, attrs, defStyleAttr);
     49         setOnEditorActionListener(this);
     50     }
     51 
     52     public void setEditTextEvenListener(EditTextEventListener listener) {
     53         mEditListener = listener;
     54     }
     55 
     56     @Override
     57     public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
     58         if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER))
     59                 || (actionId == EditorInfo.IME_ACTION_DONE)) {
     60             mEditListener.textEdited(this);
     61         }
     62         // Necessary to return false even when event handled for soft-keyboard to be dismissed
     63         // Differs from on click listener chains where first listener to handle returns true
     64         return false;
     65     }
     66 
     67     @Override
     68     public boolean onKeyPreIme(int keyCode, KeyEvent event) {
     69         if (event.getKeyCode() == KeyEvent.KEYCODE_BACK
     70                 && event.getAction() == KeyEvent.ACTION_UP) {
     71             mEditListener.textEdited(this);
     72         }
     73         return super.onKeyPreIme(keyCode, event);
     74     }
     75 }