继承EditText实现一些文本操作

我想实现EditText获得焦点和文本将要变化时做一些操作,但是下面的代码 onBeginBatchEdit 这种的执行不是预期的,大家指点一下应该怎么做?

package com.example.ebaypricing.app.component;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;
/**
 * Created by xinyihai on 2014/8/19.
 */
public class NumberEditText extends EditText {
    TextWatcher textWatcher;
    public NumberEditText(android.content.Context context) {
        super(context);
    }
    public NumberEditText(android.content.Context context, android.util.AttributeSet attrs) {
        super(context, attrs);
    }
    public NumberEditText(android.content.Context context, android.util.AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    @Override
    protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
        super.onTextChanged(text, start, lengthBefore, lengthAfter);
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (textWatcher == null){
            textWatcher = new TextWatcher(){
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
                    Log.i("NumberEditText", "beforeTextChanged");
                }
                @Override
                public void afterTextChanged(Editable editable) {
                    Log.i("NumberEditText", "afterTextChanged");
                }
                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
                    Log.i("NumberEditText", "onTextChanged");
                }
            };
            this.addTextChangedListener(textWatcher);
        }
    }
    @Override
    public void onBeginBatchEdit() {
//        Log.i("NumberEditText", "onBeginBatchEdit");
        super.onBeginBatchEdit();
    }
    @Override
    public void onEndBatchEdit() {
//        Log.i("NumberEditText", "onEndBatchEdit");
        super.onEndBatchEdit();
    }
    @Override
    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
        Log.i("NumberEditText", ("onFocusChanged:"+String.valueOf(focused) +" - "+ this.getTag()));
        super.onFocusChanged(focused, direction, previouslyFocusedRect);
    }
}
阅读 6.1k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
101 新手上路
子站问答
访问
宣传栏