2017-06-21 2 views
0

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:

  1. 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:

  1. entrée "12345" Sortie ""
  2. entrée "teststring123" Sortie "teststring123",
  3. 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(); 
    } 
} 
} 

Répondre

0

Ok, j'ai trouvé la solution, ça marche pour moi.

Pour manipuler des nombres de les transmettre et EMR à l'objet modifiable, par InputConnection, tout ce que vous devez faire ce qui suit:

Mettre en œuvre la méthode « sendKeyEvent » comme indiqué ci-dessous

@Override 
    public InputConnection onCreateInputConnection(EditorInfo outAttrs) { 

    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 sendKeyEvent(KeyEvent event) { 

      if(event.getAction() == KeyEvent.ACTION_DOWN 
        && event.getKeyCode() >= KeyEvent.KEYCODE_0 
        && event.getKeyCode() <= KeyEvent.KEYCODE_9) { 
       char c = event.getKeyCharacterMap().getNumber(event.getKeyCode()); 
       commitText(String.valueOf(c), 1); 
      } 
      return super.sendKeyEvent(event); 
     } 
    }; 
}