我有一个EditText对话框。 EditText在创建时已经填充。当用户将光标放置在文本的特定部分或其附近时,Toast将弹出。
我的问题是监听光标位置的变化。另一个post提出同样的问题,并且接受的解决方案是
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)
为我工作的解决方案
嗨,大师,谢谢你的回复,它的工作。
如果有人有兴趣,这是我做的详细的… *
第一步:创建子类
package com.example;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.EditText;
import android.widget.Toast;
public class EditTextCursorWatcher extends EditText {
public EditTextCursorWatcher(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}
public EditTextCursorWatcher(Context context, AttributeSet attrs) {
super(context, attrs);
}
public EditTextCursorWatcher(Context context) {
super(context);
}
@Override
protected void onSelectionChanged(int selStart, int selEnd) {
Toast.makeText(getContext(), "selStart is " + selStart + "selEnd is " + selEnd, Toast.LENGTH_LONG).show();
}
}
第二步:参考布局文件中的类(例如main.xml(虽然我的是一个自定义的对话框布局))。不要忘记使用完整的包名(在本例中为com.example.EditTextCursorWatcher,例如
android:id="@+id/etEdit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:minLines="5"
android:inputType="textMultiLine"/>