Почему requestFocus вызывается несколько раз
Пытаюсь переопределить requestFocus в EditText. Он вызывается несколько раз почему как сделать чтобы он 1 раз вызывался? В принципе я могу поставить в requestFocus таймер и только через определенное время его выполнять (скорее так и сделаю), но может есть что еще?
public class MEditText extends EditText {
private boolean isOpenKeyboard;
private boolean isMIsfocused = false;
public MEditText(Context context) {
super(context);
}
public MEditText(Context context, AttributeSet attrs) {
super(context, attrs);
mpaint = new Paint();
TypedArray a = context.getTheme().obtainStyledAttributes(attrs,
R.styleable.MEditText, 0, 0);
try {
//get the text and colors specified using the names in attrs.xml
isOpenKeyboard = a.getBoolean(MEditText_isOpenKeyboard, false);
} finally {
a.recycle();
}
}
@Override
public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
isMIsfocused = true;
if(isOpenKeyboard) { //был нажат enter показываем клаву
isOpenKeyboard = false;
final InputMethodManager imm = ((InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE));
imm.showSoftInput(this, 0);
return super.requestFocus(direction, previouslyFocusedRect);
} else {
onFocusChanged(true, direction, previouslyFocusedRect);
}
getContext().getSystemService(Context.INPUT_METHOD_SERVICE));
return true;
}
private void applyKeyboardShowHide(boolean autofocus) {
final InputMethodManager imm = ((InputMethodManager) getContext()
.getSystemService(Context.INPUT_METHOD_SERVICE));
boolean isEnterWasPressed = false; //если isEnterWasPressed=true не выводиться если isEnterWasPressed=fals выводиться
if (imm != null) {
if(isEnterWasPressed) {
if(imm.isActive(this)) {
imm.hideSoftInputFromWindow(getApplicationWindowToken(), 0);
}
}else if(autofocus) {
imm.showSoftInput(this, 0);
}
}
}
@Override
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
super.onFocusChanged(focused, direction, previouslyFocusedRect);
if(focused) {
setBackgroundColor(getResources().getColor(R.color.purple_200));
setTextColor(getResources().getColor(R.color.black));
} else {
isMIsfocused = false;
setBackgroundColor(getResources().getColor(R.color.teal_200));
setTextColor(getResources().getColor(R.color.black));
}
}
@Override
public void extendSelection(int index) {
super.extendSelection(index);
}
public void showKeyboard() {
isOpenKeyboard = true;
if(isMIsfocused) {
requestFocus();
}
}
public void hideKeyboard() {
isOpenKeyboard = false;
}
public boolean getIsOpenKeyboard() {
return isOpenKeyboard;
}
public boolean getIsMFocused() {
return isMIsfocused;
}
}