EditText hint内容居右,光标居右

开发中,我们往往会遇到这样布局

 但是EdiText 光标也同时居依然居左,所以我们需要自定义实现,光标居右。

代码:

public class CursorRightEditText extends LinearLayout {

    private AppCompatEditText editText;
    private TextView tvHint;
    private TextWatcher textWatcher;

    public CursorRightEditText(Context context) {
        this(context, null);
    }

    public CursorRightEditText(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CursorRightEditText(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CursorRightEditText);
        String crtHint = typedArray.getString(R.styleable.CursorRightEditText_crt_hint);
        int crtTextColorHint = typedArray.getColor(R.styleable.CursorRightEditText_crt_textColorHint, ContextCompat.getColor(context, R.color.common_text_888888));
        int crtTextSize = typedArray.getDimensionPixelSize(R.styleable.CursorRightEditText_crt_textSize, dp2px(context, 12));
        int crtTextColor = typedArray.getColor(R.styleable.CursorRightEditText_crt_textColor, ContextCompat.getColor(context, R.color.common_text_888888));
        typedArray.recycle();
        LayoutInflater.from(context).inflate(R.layout.common_view_cursor_right_edit_text, this);
        editText = findViewById(R.id.cursor_right_editText_et_content);
        tvHint = findViewById(R.id.cursor_right_editText_tv_hint);
        if (crtHint != null && crtHint.length() > 0) {
            tvHint.setVisibility(View.VISIBLE);
            tvHint.setText(crtHint);
            tvHint.setTextColor(crtTextColorHint);
            tvHint.setTextSize(px2dp(context, crtTextSize));
        } else {
            tvHint.setVisibility(View.GONE);
        }
        editText.setTextColor(crtTextColor);
        editText.setTextSize(px2dp(context, crtTextSize));

        tvHint.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                editText.setFocusable(true);
                editText.setFocusableInTouchMode(true);
                KeyBoardUtils.showInput(editText, context);
            }
        });

        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                if (textWatcher != null) {
                    textWatcher.beforeTextChanged(s, start, count, after);
                }
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (textWatcher != null) {
                    textWatcher.onTextChanged(s, start, count, count);
                }
                if (s != null && s.length() > 0) {
                    tvHint.setVisibility(GONE);
                } else {
                    tvHint.setVisibility(VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (textWatcher != null) {
                    textWatcher.afterTextChanged(s);
                }
            }
        });
    }


    public void addTextChangedListener(TextWatcher TextWatcher) {
        this.textWatcher = textWatcher;
    }

    public Editable getText() {
        return editText.getText();
    }

    public EditText getEditText() {
        return editText;
    }

    public void setText(CharSequence text) {
        if (text != null && text.length() > 0) {
            tvHint.setVisibility(GONE);
        } else {
            tvHint.setVisibility(VISIBLE);
        }
        editText.setText(text);
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dp2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
     */
    public static int px2dp(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }
}

 attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- 内容居右,光标居右的EditText -->
    <declare-styleable name="CursorRightEditText">
        <attr name="crt_textColorHint" format="color" />
        <attr name="crt_hint" format="string" />
        <attr name="crt_textSize" format="dimension" />
        <attr name="crt_textColor" format="color" />
    </declare-styleable>

</resources>

layout:common_view_cursor_right_edit_text

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <androidx.appcompat.widget.AppCompatEditText
        android:id="@+id/cursor_right_editText_et_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@null"
        android:gravity="right|center_vertical" />

    <TextView
        android:id="@+id/cursor_right_editText_tv_hint"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="2dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

如此我们即可实现hint内容居右,光标居右。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值