android怎么实现点击空白编辑框选择内容
时间: 2023-06-04 15:07:45 浏览: 39
要实现点击空白编辑框选择内容,可以使用以下代码:
```
EditText editText = findViewById(R.id.edit_text);
editText.setShowSoftInputOnFocus(false); //禁止弹出系统输入框
editText.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (MotionEvent.ACTION_UP == event.getAction()) {
//获取点击的位置
int x = (int) event.getX();
int y = (int) event.getY();
//获取位置对应的文本偏移量
int offset = editText.getOffsetForPosition(x, y);
//设置选中文本
editText.setSelection(offset);
}
return true;
}
});
```
这里通过设置EditText的OnTouchListener监听触摸事件,处理点击事件。首先禁止弹出系统输入框,然后获取点击位置的文本偏移量,最后设置选中的文本。
相关推荐
![apk](https://img-home.csdnimg.cn/images/20210720083646.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![crx](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)