mirror of
https://github.com/AngelAuraMC/Amethyst-Android.git
synced 2025-09-10 05:01:29 -04:00
Fix[touch_char_input]: use TextWatcher instead of onTextChanged override
Avoids a bug on some devices caused by setText called by the framework early
This commit is contained in:
parent
b28fc4a1f4
commit
3e42bfd21f
@ -5,6 +5,7 @@ import static android.content.Context.INPUT_METHOD_SERVICE;
|
|||||||
|
|
||||||
import android.annotation.SuppressLint;
|
import android.annotation.SuppressLint;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.text.Editable;
|
||||||
import android.util.AttributeSet;
|
import android.util.AttributeSet;
|
||||||
import android.view.KeyEvent;
|
import android.view.KeyEvent;
|
||||||
import android.view.inputmethod.InputMethodManager;
|
import android.view.inputmethod.InputMethodManager;
|
||||||
@ -34,31 +35,6 @@ public class TouchCharInput extends androidx.appcompat.widget.AppCompatEditText
|
|||||||
private boolean mIsDoingInternalChanges = false;
|
private boolean mIsDoingInternalChanges = false;
|
||||||
private CharacterSenderStrategy mCharacterSender;
|
private CharacterSenderStrategy mCharacterSender;
|
||||||
|
|
||||||
/**
|
|
||||||
* We take the new chars, and send them to the game.
|
|
||||||
* If less chars are present, remove some.
|
|
||||||
* The text is always cleaned up.
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
|
|
||||||
super.onTextChanged(text, start, lengthBefore, lengthAfter);
|
|
||||||
if(mIsDoingInternalChanges)return;
|
|
||||||
if(mCharacterSender != null){
|
|
||||||
for(int i=0; i < lengthBefore; ++i){
|
|
||||||
mCharacterSender.sendBackspace();
|
|
||||||
}
|
|
||||||
|
|
||||||
for(int i=start, count = 0; count < lengthAfter; ++i){
|
|
||||||
mCharacterSender.sendChar(text.charAt(i));
|
|
||||||
++count;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//Reset the keyboard state
|
|
||||||
if(text.length() < 1) clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* When we change from app to app, the keyboard gets disabled.
|
* When we change from app to app, the keyboard gets disabled.
|
||||||
* So, we disable the object
|
* So, we disable the object
|
||||||
@ -142,6 +118,9 @@ public class TouchCharInput extends androidx.appcompat.widget.AppCompatEditText
|
|||||||
|
|
||||||
/** This function deals with anything that has to be executed when the constructor is called */
|
/** This function deals with anything that has to be executed when the constructor is called */
|
||||||
private void setup(){
|
private void setup(){
|
||||||
|
// Using TextWatcher instead of overriding onTextChanged because some Huawei firmware
|
||||||
|
// calls setText in constructor, causing havoc for our listener
|
||||||
|
addTextChangedListener(new InputTextWatcher());
|
||||||
setOnEditorActionListener((textView, i, keyEvent) -> {
|
setOnEditorActionListener((textView, i, keyEvent) -> {
|
||||||
sendEnter();
|
sendEnter();
|
||||||
clear();
|
clear();
|
||||||
@ -151,5 +130,38 @@ public class TouchCharInput extends androidx.appcompat.widget.AppCompatEditText
|
|||||||
clear();
|
clear();
|
||||||
disable();
|
disable();
|
||||||
}
|
}
|
||||||
|
private class InputTextWatcher implements android.text.TextWatcher {
|
||||||
|
@Override
|
||||||
|
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* We take the new chars, and send them to the game.
|
||||||
|
* If less chars are present, remove some.
|
||||||
|
* The text is always cleaned up.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
|
||||||
|
if(mIsDoingInternalChanges) return;
|
||||||
|
if(mCharacterSender != null){
|
||||||
|
for(int i=0; i < lengthBefore; ++i){
|
||||||
|
mCharacterSender.sendBackspace();
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int i=start, count = 0; count < lengthAfter; ++i){
|
||||||
|
mCharacterSender.sendChar(text.charAt(i));
|
||||||
|
++count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void afterTextChanged(Editable editable) {
|
||||||
|
if(mIsDoingInternalChanges) return;
|
||||||
|
// Moved from onTextChanged because "It is an error to attempt to make changes to s from this callback."
|
||||||
|
// reference: https://developer.android.com/reference/android/text/TextWatcher#onTextChanged(java.lang.CharSequence,%20int,%20int,%20int)
|
||||||
|
if(editable.length() < 1) clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user