【发布时间】:2017-06-21 00:03:50
【问题描述】:
当我将键盘类型设置为 TYPE_CLASS_NUMBER 时,我遇到了一种奇怪的键盘行为。
我使用 BaseInputConnection 连接我的可编辑对象和 TextWathcer 以监听任何文本更改。
问题:
当 inputType=TYPE_CLASS_TEXT 并且第一个输入符号是 any 除数字外的符号 - 一切正常,可编辑文本更改。
当我将键盘类型切换为 TYPE_CLASS_NUMBER 并尝试 输入数字 - 没有任何变化。
例如:
- 输入“12345”输出“”
- 输入“teststring123”输出“teststring123”,
- 输入“123teststring”输出“teststring”
完整代码:
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();
}
}
}
【问题讨论】:
标签: android android-custom-view android-keypad