Browse Source

add backspace behavior

pull/45/head
Thodoris1999 6 years ago
parent
commit
fe9147d490
  1. 31
      app/src/main/java/gr/thmmy/mthmmy/utils/EmojiKeyboard.java

31
app/src/main/java/gr/thmmy/mthmmy/utils/EmojiKeyboard.java

@ -1,17 +1,18 @@
package gr.thmmy.mthmmy.utils; package gr.thmmy.mthmmy.utils;
import android.content.Context; import android.content.Context;
import android.os.Handler;
import android.support.v7.widget.AppCompatImageButton; import android.support.v7.widget.AppCompatImageButton;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.SparseArray; import android.util.SparseArray;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.inputmethod.InputConnection; import android.view.inputmethod.InputConnection;
import android.widget.GridView; import android.widget.GridView;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.R;
import timber.log.Timber;
public class EmojiKeyboard extends LinearLayout { public class EmojiKeyboard extends LinearLayout {
@ -144,12 +145,30 @@ public class EmojiKeyboard extends LinearLayout {
inputConnection.commitText(value, 1); inputConnection.commitText(value, 1);
}); });
AppCompatImageButton backspaceButton = (AppCompatImageButton) findViewById(R.id.backspace_button); AppCompatImageButton backspaceButton = (AppCompatImageButton) findViewById(R.id.backspace_button);
backspaceButton.setOnClickListener(view -> { // backspace behavior
CharSequence selectedText = inputConnection.getSelectedText(0); final Handler handler = new Handler();
if (TextUtils.isEmpty(selectedText)) Runnable longPressed = new Runnable() {
@Override
public void run() {
inputConnection.deleteSurroundingText(1, 0); inputConnection.deleteSurroundingText(1, 0);
else handler.postDelayed(this, 50);
inputConnection.commitText("", 1); }
};
backspaceButton.setOnTouchListener((v, event) -> {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
CharSequence selectedText = inputConnection.getSelectedText(0);
if (TextUtils.isEmpty(selectedText))
inputConnection.deleteSurroundingText(1, 0);
else
inputConnection.commitText("", 1);
handler.postDelayed(longPressed, 400);
break;
case MotionEvent.ACTION_UP:
handler.removeCallbacks(longPressed);
break;
}
return true;
}); });
} }

Loading…
Cancel
Save