Last active
June 12, 2020 09:53
-
-
Save xzzz9097/b5c630aa29b511a90b4f to your computer and use it in GitHub Desktop.
Android EditText with key back event listener
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
package com.edge.utils; | |
import android.content.Context; | |
import android.util.AttributeSet; | |
import android.view.KeyEvent; | |
import android.view.MenuItem; | |
import android.view.inputmethod.InputMethodManager; | |
import android.widget.EditText; | |
import android.widget.ListView; | |
public class CustomEditText extends EditText { | |
OnKeyboardHidden mOnKeyboardHidden; | |
public CustomEditText(Context context) | |
{ | |
super(context); | |
init(); | |
} | |
public CustomEditText(Context context, AttributeSet attrs) | |
{ | |
super(context, attrs); | |
init(); | |
} | |
public CustomEditText(Context context, AttributeSet attrs, int defStyle) | |
{ | |
super(context, attrs, defStyle); | |
init(); | |
} | |
private void init() | |
{ } | |
public interface OnKeyboardHidden { | |
public void onKeyboardHidden(); | |
} | |
public void setOnKeyboardHidden(OnKeyboardHidden action) { | |
mOnKeyboardHidden = action; | |
} | |
@Override | |
public boolean onKeyPreIme(int keyCode, KeyEvent event) { | |
if (keyCode == KeyEvent.KEYCODE_BACK) { | |
// User has pressed Back key. So hide the keyboard | |
InputMethodManager imm = (InputMethodManager) getContext() | |
.getSystemService(Context.INPUT_METHOD_SERVICE); | |
imm.hideSoftInputFromWindow(this.getWindowToken(), 0); | |
mOnKeyboardHidden.onKeyboardHidden(); | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment