1. 首先继承EditText
2. 并重写onTouchEvent方法
public boolean onTouchEvent(MotionEvent event)
{
Editable ed=this.getEditableText();
//获得action
int action = event.getAction();
//用来显示文本的Layout布局
Layout layout = this.getLayout();
//获得第几行
int pointY = layout.getLineForVertical(getScrollY()+(int)event.getY());
switch(action)
{
case MotionEvent.ACTION_DOWN:
/* 手指按下时记忆起始点,第一个是行数,第二个是开始点 */
startPoint=layout.getOffsetForHorizontal(pointY,event.getX());
//第一个参数Editable是子类 , 第二个参数是开始点
Selection.setSelection(ed, startPoint);
break;
case MotionEvent.ACTION_MOVE:
/* 手指移动时记忆終止点 */
endPoint=layout.getOffsetForHorizontal(pointY,event.getX());
Selection.setSelection(ed,startPoint,endPoint);
break;
}
return true;
}
3. 根据上面的startPoint与endPoint截取字符串
public String getSelectedText() { return this.getText().toString().substring(startPoint,endPoint); }
4. 获得 ClipboardManager
ClipboardManage cManager = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
5. 复制已截取字符串 ev是EditeText的实例
cManager.setText(ev.getSelectedText());
6. 获得 已复制字符串
setText(cManager.getText());