我在MainActivity中有EditText,我想得到editext的光标位置我有edittext子类并且覆盖了下面的方法。
@override
protected void onSelectionChanged(int selStart, int selEnd) {
OnSelectionChangedListener LISTENER = new MainActivity();
LISTENER.update_pos(selStart, selEnd);
super.onSelectionChanged(selStart, selEnd);
}
interface OnSelectionChangedListener {
void update_pos(int selStart, int SelEnd);
}
我已经使用了接口并在My MainActivity中获得了edittext selStart和selEnd的值,但我想在每个选择更改的TextView中设置值
这应该做的伎俩:
public class CursorEditText extends AppCompatEditText {
private OnSelectionChangedListener mListener;
public CursorEditText(Context context) {
super(context);
}
public CursorEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CursorEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public void setOnSelectionChangedListener(OnSelectionChangedListener onSelectionChangedListener) {
mListener = onSelectionChangedListener;
}
@Override
protected void onSelectionChanged(int selStart, int selEnd) {
if (mListener != null)
mListener.onSelectionChanged(selStart, selEnd);
super.onSelectionChanged(selStart, selEnd);
}
public interface OnSelectionChangedListener {
void onSelectionChanged(int selStart, int selEnd);
}
}
然后在您的活动中,实现OnSelectionChangedListener并调用:
yourEditTextReference.setOnSelectionChangedListener(this);
此方法现在应该可以在您的活动中实现:
@Override
public void onSelectionChanged(int selStart, int selEnd) {
yourTextView.setText(String.valueOf(selStart));
}