Android EditText listener for cursor position change(用于光标位置更改的 Android EditText 侦听器)
问题描述
我有一个带有 EditText 的对话框.EditText 在创建时已被填充.当用户将光标放在文本的某些部分上或附近时,会弹出一个 Toast.
I have a dialog with EditText in it. The EditText is already populated when it is created. When the user places the cursor on or near certain parts of the text a Toast will pop up.
我的问题是监听光标位置的变化.另一个 post 提出了同样的问题,并且接受的解决方案是
My problem is listening for changes in cursor position. Another post asks the same question and the accepted solution was
您可以覆盖 onSelectionChanged (int selStart, int selEnd) 以获取有关选择更改的通知.如果光标被移动,这也会被调用(在这种情况下 selStart == selEnd)
You can override onSelectionChanged (int selStart, int selEnd) to get notified about selection changes. If the cursor is moved, this is called as well (in this case selStart == selEnd)
onSelectionChanged (int selStart, int selEnd) 是 TextView 类的受保护方法.如何覆盖它?
onSelectionChanged (int selStart, int selEnd) is a protected method of the TextView class. How do override it?
推荐答案
只需继承或扩展类 EditText 并将以下代码添加到新创建的类中:
Just subclass or extend the class EditText and add the following code to the newly create class:
@Override
protected void onSelectionChanged(int selStart, int selEnd) {
// Do ur task here.
}
不要忘记向子类添加构造函数.:)
Don't forget to add constructors to the subclass. :)
这篇关于用于光标位置更改的 Android EditText 侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:用于光标位置更改的 Android EditText 侦听器
基础教程推荐
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
