Android - EditText设置前缀始终可见

时间:2017-09-28 11:41:37

标签: android

我制作了一个自定义的EditText,可以添加前缀,这项工作做得很好,但我希望我的前缀保持可见,即使输入的文字大小太大。

Here您可以看到带有前缀的EditText here你可以看到我面临的问题。

这是我的Edittext类:

public class MyEditText extends android.support.v7.widget.AppCompatEditText
{
    private String mPrefix;
    private Rect mPrefixRect = new Rect();

    public void setPrefix(final String prefix)
    {
        mPrefix = prefix;
    }

    public MyEditText(final Context context)
    {
        super(context);
        mPrefix = "";
    }

    public MyEditText(final Context context, AttributeSet attributeSet)
    {
        super(context, attributeSet)
        TypedArray a = context.getTheme().obtainStyledAttributes(attributeSet, R.styleable.MyEditText, 0, 0);
        mPrefix = a.getString(R.styleable.MyEditText_prefix);
        if (mPrefix == null)
            mPrefix = "";
        a.recycle();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        getPaint().getTextBounds(mPrefix, 0, mPrefix.length(), mPrefixRect);
        if (!mPrefix.matches(""))
            mPrefixRect.right += getPaint().measureText("  "); // add some offset
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);
        Paint paint = getPaint();
        paint.setColor(Color.BLACK);
        canvas.drawText(mPrefix, super.getCompoundPaddingLeft(), getBaseline(), paint);
    }

    @Override
    public int getCompoundPaddingLeft()
    {
        return super.getCompoundPaddingLeft() + mPrefixRect.width();
    }
}

修改

根据要求提供了我的R.styleable.MyEditText:

<resources>
    <declare-styleable name="MyEditText">
        <attr name="prefix" format="string" />
        <attr name="parentLayout" format="reference"/>
    </declare-styleable>
</resources>

我的XML文件中我正在使用MyEditText:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="thurluth.popups.MainActivity"
    android:id="@+id/main_layout">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <MyEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:prefix="Login :"
        app:parentLayout="@id/main_layout"
        android:layout_toEndOf="@+id/button"
        android:inputType="text"/>

</RelativeLayout>

对于那些我遇到同样问题的人,有解决方案:

您必须覆盖自定义EditText中的onDraw函数

@Override
    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);
        Paint paint = getPaint();
        paint.setColor(Color.BLACK);
        float prefixPosX = 10;  //I added some offset at the left the prefix
        if (paint.measureText(getText().toString()) > getWidth() - (paint.measureText(mPrefix + "  ")) - 10)
            prefixPosX = paint.measureText(getText().toString() + offset) - (getWidth() - (paint.measureText(mPrefix + "  ")));
        canvas.drawText(mPrefix, prefixPosX, getBaseline(), paint);
    }

1 个答案:

答案 0 :(得分:1)

由于它是您自己的View,您可以覆盖onDraw方法。 传递的Canvas包含一个名为drawText的方法。设置它的位置,创建所需的Paint - 对象,让它变得很酷并且你很高兴: - )