网上找了很多资料,基本可以实现希望的效果,但还是不能解决双击后,光标跳转
最后还是拦截了OnTouchListener,但是光标又不能跳转,在onTouch中使用了【editText.getOffsetForPosition】取得位置后,调用【editText.setSelection()】
实现了光标移到。
。。。
editText.setOnTouchListener(new onDoubleClick());
。。。。
}
class onDoubleClick implements View.OnTouchListener{
int count = 0;
int firClick = 0;
int secClick = 0;
@Override
public boolean onTouch(View v, MotionEvent event) {
final float x = event.getX();
final float y = event.getY();
if(MotionEvent.ACTION_DOWN == event.getAction()){
int temp = editText.getOffsetForPosition(x,y);
editText.setSelection(temp);
}
return true;
}
XML中的设置
<EditText
android:id="@+id/textNum"
android:layout_x="160px"
android:layout_y="40px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="48px"
android:textStyle="normal"
android:textColor="#FFFFFFFF"
android:background="#00FFFFFF"
android:text="132121212212"
android:maxLength="99"
android:focusable="true"
android:clickable="true"
android:textIsSelectable="false"
android:focusableInTouchMode="true"
android:longClickable="false"
android:contextClickable="false"
android:linksClickable="false"
android:nestedScrollingEnabled="false"
android:hapticFeedbackEnabled="false"
android:cursorVisible="true"
android:textSelectHandle="@drawable/edit_select_handle"
android:selectAllOnFocus="false"
/>
edit_select_handle.xml - 这是在网上找到的资料<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <size android:width="0dp" /> <!-- 实际不显示,该图片的作用仅是占位,因直接设置android:textSelectHandle="@null"运行时出错,当然根据具体情况,可设置成一张图片png等 --> </shape>
实现这种程度后期发现,拖动edit框内的文字无法scroll滚动,因为设定了OnTouchListener,因此只能自己在OnTouchListener调用响应方法实现文字滚动class onDoubleClick implements View.OnTouchListener{ int firClick = 0; int secClick = 0; float dx; float dy; @Override public boolean onTouch(View v, MotionEvent event) { final float x = event.getX(); final float y = event.getY(); Log.i(TAG,"onTouch:"+event.getAction()); if(MotionEvent.ACTION_DOWN == event.getAction()){ Log.i(TAG,"dx:"+dx); Log.i(TAG,"x:"+x); dx = x; dy = y; } if (MotionEvent.ACTION_UP == event.getAction()){ editText.requestFocus(); int temp = editText.getOffsetForPosition(x,y); editText.setSelection(temp); } if(MotionEvent.ACTION_MOVE == event.getAction()){ editText.scrollBy((int) (dx - x ),0); ViewGroup.LayoutParams layoutParams = v.getLayoutParams(); dx = x; dy = y; editText.requestLayout(); } return true; } }
刚接触android开发,前辈有更好的办法请明示,3Q
希望能给有这方法需求的朋友提供帮助