J'ai été confronté à un étrange comportement du clavier lorsque j'ai configuré le type de clavier sur TYPE_CLASS_NUMBER. J'utilise BaseInputConnection pour connecter mon objet éditable et TextWathcer pour écouter les changements de texte.Android: affichage personnalisé avec le clavier TYPE_CLASS_NUMBER (IME) - l'entrée ne fonctionne pas
Le problème:
Lorsque la inputType = TYPE_CLASS_TEXT et premier symbole d'entrée est un symbole exception du numéro - tous les travaux texte change fins et modifiables. Lorsque je commute le type de clavier sur TYPE_CLASS_NUMBER et que j'essaie d'entrer , entrez des chiffres - rien ne change.
Par exemple:
- entrée "12345" Sortie ""
- entrée "teststring123" Sortie "teststring123",
- entrée "123teststring" Sortie "testString"
Code produit:
public class CustomView extends View implements View.OnClickListener {
private Editable editable;
private InputMethodManager imm;
private final CustomTextWatcher textWatcher = new CustomTextWatcher();
private Paint mainPaint;
private int batch;
public CustomView(Context context) {
super(context);
init(context, null);
}
public CustomView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public CustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
private void init(Context context, @Nullable AttributeSet attrs) {
setOnClickListener(this);
setFocusableInTouchMode(true);
imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
editable = Editable.Factory.getInstance().newEditable("");
editable.setSpan(textWatcher, 0, editable.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
Selection.setSelection(editable, 0);
mainPaint = new Paint();
mainPaint.setColor(ContextCompat.getColor(context, R.color.colorPrimary));
mainPaint.setTextSize(30);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawText(editable.toString(), 15, 20, mainPaint);
super.onDraw(canvas);
}
@Override
public void onClick(View v) {
imm.showSoftInput(this, 0);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP && batch == 0) {
int cursorPosition = 0;
imm.viewClicked(this);
imm.updateSelection(this, cursorPosition, cursorPosition, cursorPosition, cursorPosition);
imm.updateSelection(this, cursorPosition, cursorPosition, -1, -1);
}
return super.onTouchEvent(event);
}
@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
Log.e("CustomView", " onCreateInputConnection");
outAttrs.inputType = InputType.TYPE_CLASS_NUMBER;
outAttrs.imeOptions = EditorInfo.IME_ACTION_DONE;
outAttrs.initialSelStart = 0;
outAttrs.initialSelEnd = 0;
return new BaseInputConnection(this, true) {
@Override
public Editable getEditable() {
return editable;
}
@Override
public boolean endBatchEdit() {
batch++;
return super.endBatchEdit();
}
@Override
public boolean beginBatchEdit() {
batch--;
return super.beginBatchEdit();
}
};
}
@Override
public boolean onCheckIsTextEditor() {
return true;
}
private class CustomTextWatcher implements TextWatcher {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
Log.e("CustomView", "afterTextChanged " + s);
invalidate();
}
}
}