用于光标位置更改的 Android EditText 侦听器 [英] Android EditText listener for cursor position change

查看:19
本文介绍了用于光标位置更改的 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 侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆