0
キーボードタイプをTYPE_CLASS_NUMBERに設定すると、私は奇妙なキーボードの動作に直面しました。Android:TYPE_CLASS_NUMBERキーボード(IME)でのカスタム表示 - 入力が機能しない
BaseInputConnectionを使用して、編集可能なオブジェクトとTextWathcerを接続して、テキストの変更をリッスンします。
問題:
inputType = TYPE_CLASS_TEXTと最初の入力記号が番号以外の任意の シンボルである - すべての罰金と編集可能なテキストの変更を動作します。
キーボードタイプをTYPE_CLASS_NUMBERに切り替えて、 に数字を入力すると、何も変わりません。
例えば
:
- 入力 "たTestString "12345" 出力 ""
- 入力 "teststring123" 出力 "teststring123"、
- 入力 "" 出力" を123teststring
フルコード:
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();
}
}
}