android EditText 设置android:gravity="right|center_vertical",获取焦点时 光标不在最后? 复制下面代码即可
<com....DuLastInputEditText
android:id="@+id/presale_et"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@null"
android:digits="0123456789"
android:gravity="right|center_vertical"
android:hint="请输入"
android:inputType="number"
android:maxLength="6"
android:paddingRight="5dp"
android:textColor="@color/color_c33"
android:textColorHint="@color/color_99"
android:textSize="13dp" />
public class DuLastInputEditText extends EditText {
public DuLastInputEditText(Context context) {
super(context);
}
public DuLastInputEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public DuLastInputEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
super.onFocusChanged(focused, direction, previouslyFocusedRect);
if (focused) {
setCursorVisible(false);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
setCursorVisible(true);
if (getText() == null) {//判空,防止出现空指针
setSelection(0);
} else {
setSelection(getText().length()); // 保证光标始终在最后面
}
}
}, 200);
}
}
@Override
protected void onSelectionChanged(int selStart, int selEnd) {
super.onSelectionChanged(selStart, selEnd);
// if (selStart == selEnd) {//防止不能多选
// if (getText() == null) {//判空,防止出现空指针
// setSelection(0);
// } else {
// setSelection(getText().length()); // 保证光标始终在最后面
// }
//
// }
}