需要上,EditText是有内容的,用户点击后就全选,最终达到方便编辑的效果。

方案一:

百度了一下,原来的思路是通过点击事件实现,但是效果很差,有时间点击中间能全选,有时候只是选中光标而已。估计是第一次点击触发touch,第二次点击才触发click.

方案二:

通过EditText的属性,调整焦点,达到全选的效果。同样可以通过setOnTouchListener的代码实现,而touch的UP或者DOWN我自己测试时,都而已实现全选.

代码实现:

editText.setOnTouchListener(new OnTouchListener() {

@Override

public boolean onTouch(View view, MotionEvent event) {

if (event.getAction() == MotionEvent.ACTION_UP) {

editText.setSelectAllOnFocus(true);

editText.selectAll();

}

return false;

}

});

xml属性的方式:

android:selectAllOnFocus="true"

android:singleLine="true"

android:id="@+id/edit_1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:focusable="true"

android:focusableInTouchMode="true"

android:text="获取焦点" />

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_marginLeft="5dp"

android:hint="手机号"

android:selectAllOnFocus="true"

android:singleLine="true"

android:text="123456"

android:textSize="18sp" />

最后,EditText的处理,都需要取消获取焦点,如果不处理,它会自动获取焦点了

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐