关闭

自定义EditText控件

3101人阅读 评论(0) 收藏 举报

大家都知道 系统的EditText长按都是有 复制、粘贴等等的Menu出现 如果想去除

代码如下:

public class TextPage extends EditText
{
    private int off; 
    
    public TextPage(Context context, AttributeSet attributeSet)
    {
        super(context, attributeSet);
    }
    
    public TextPage(Context context)
    {
        super(context);
        initView();
    }
    
    private void initView()
    {
        setGravity(Gravity.TOP);
        setBackgroundColor(Color.WHITE);
    }
    
    @Override
    protected void onCreateContextMenu(ContextMenu menu)
    {
        //阻止长按弹出Menu菜单
    }
    
    @Override
    public boolean getDefaultEditable()
    {
        return false;
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        int action = event.getAction();
        Layout layout = getLayout();
        int line = 0;
        switch (action)
        {
            case MotionEvent.ACTION_DOWN:
                line = layout.getLineForVertical(getScrollY() + (int)event.getY());
                off = layout.getOffsetForHorizontal(line, (int)event.getX());
                Selection.setSelection(getEditableText(), off);
                break;
            case MotionEvent.ACTION_MOVE:
            case MotionEvent.ACTION_UP:
                line = layout.getLineForVertical(getScrollY() + (int)event.getY());
                int curOff = layout.getOffsetForHorizontal(line, (int)event.getX());
                Selection.setSelection(getEditableText(), off, curOff);
                break;
        }
        return true;
    }
}

 

如果有别的什么需求说不定也会用到的。


转载:http://chroya.iteye.com/blog/753634

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场