Code example for EditText

Methods: setOnEditorActionListener, setOnKeyListener

0
    private EditUtils() { 
        // utility class 
    } 
 
    public static void setActionListener(final EditText editText, final Runnable runnable) {
        editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
 
            @Override 
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_GO) {
                    runnable.run();
                    return true; 
                } 
 
                return false; 
            } 
        }); 
 
        editText.setOnKeyListener(new View.OnKeyListener() {
 
            @Override 
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                // If the event is a key-down event on the "enter" button 
                if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {