自定义editext,可复制,隐藏键盘

本文详细介绍了如何在EditText中实现文本复制功能,并通过触摸事件优化了用户体验。包括初始化组件、获取文本偏移值、处理触摸事件以及将选定文本复制到剪贴板的过程。

/** 
 * @author zjf
 * 点击可复制文本信息 
 * */
public class TextViewCanCopy extends EditText{
	private int off; //字符串的偏移值
	
	ClipboardManager cmb = (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE);
	InputMethodManager imm = ((InputMethodManager)getContext().getSystemService(Context.INPUT_METHOD_SERVICE));
	
	public TextViewCanCopy(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initWidget();
	}

	public TextViewCanCopy(Context context, AttributeSet attrs) {
		super(context, attrs);
		initWidget();
	}

	public TextViewCanCopy(Context context) {
		super(context);
		initWidget();
	}
	
	@Override
	protected boolean getDefaultEditable() {
		return false;
	}

/*	@Override
	protected MovementMethod getDefaultMovementMethod() {
		// TODO Auto-generated method stub
		 
		return null;
	}
	 
	@Override    
    protected void onCreateContextMenu(ContextMenu menu) {    
        //不做任何处理,为了阻止长按的时候弹出上下文菜单    
    }  */
	
	protected void initWidget() {
		setFocusable(true);
		setCursorVisible(true);
//		this.setOnLongClickListener(mOnLongClickListener);
	}

/*	OnLongClickListener mOnLongClickListener = new  OnLongClickListener() {
		
		@Override
		public boolean onLongClick(View v) {
			cmb.setText(getText());
        	Toast.makeText(getContext(), "已复制文本到剪贴板", 1000).show();
			return false;
		}
		

	};*/
	
	
	int getOffsetAtText(MotionEvent event) {
		Layout layout = getLayout();
		if(layout == null) {
		return -1;
		}
		int line = layout.getLineForVertical(getScrollY() + (int) event.getY() );
		int offset = layout.getOffsetForHorizontal(line, (int) event.getX()) - 1;
		//Log.i(TAG, "line = " + line + ", offset = " + offset);
		return offset;
	}
	
	@Override    
    public boolean onTouchEvent(MotionEvent event) {    
        int action = event.getAction();    
        Layout layout = getLayout();    
        int line = 0;    
        switch(action) {    
        case MotionEvent.ACTION_DOWN:    
//        	Log.i("down000000", "action down"+off);
//            line = layout.getLineForVertical(getScrollY()+ (int)event.getY());            
//            off = layout.getOffsetForHorizontal(line, (int)event.getX());    
        	super.onTouchEvent(event);
        	off = getOffsetAtText(event);
            Selection.setSelection(getEditableText(), off);    
            
            imm.hideSoftInputFromWindow(getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            break;    
        case MotionEvent.ACTION_MOVE:    
        case MotionEvent.ACTION_UP:    
        	
//            line = layout.getLineForVertical(getScrollY()+(int)event.getY());     
//            int curOff = layout.getOffsetForHorizontal(line, (int)event.getX());    
        	int curOff = getOffsetAtText(event);
            Selection.setSelection(getEditableText(), off, curOff);    
//            Log.i("adsfsdasdsds0f0s0sf0000afs", off+ "  " + curOff);
            String str = "";
            String tmp =  getText().toString();
            
            
            if(off < curOff)
            	str = tmp.substring(off, curOff);
            else if(curOff > off)
            	str = tmp.substring(curOff, off);
            
            if(str.length() > 0) {
            	cmb.setText(str);
            	Toast.makeText(getContext(), "已复制到剪贴板", 1000).show();
            }
            
            break;    
        }  
		
        return true;    
    }
	
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值