問題描述
我有一個帶有 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 偵聽器的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!