---
title: "Editext 监听光标位置 "
author: "mxw3755"
date: "2014-12-10T17:41:22Z"
tags: ["Android", "edittext", "光标位置", "监听"]
categories: ["移动开发"]
slug: "editext"
draft: false
lastmod: "2014-12-10T17:41:09Z"
id: 2164905
url: "https://mxw3755.iteye.com/blog/2164905"
---

因为项目需要，需要实时监听光标的位置变化，网上提出的用TextWatcher和onTouchListener中调用contentText.getSelectionStart()都是获取的上一次位置。

 

只要重写onSelectionChanged就可以获取最新的光标位置了。

 

```
public class NoteContentEditText extends EditText {

	public NoteContentEditText(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onDraw(Canvas canvas) {
		super.onDraw(canvas);
	}
	
	@Override
	protected void onSelectionChanged(int selStart, int selEnd) {
		// TODO Auto-generated method stub
		super.onSelectionChanged(selStart, selEnd);
		Logg.D("onSelectionChanged selStart "+selStart+" selEnd "+selEnd);
	}
}
```