android EditText中的虚线

时间:2012-09-24 09:27:46

标签: android android-edittext dotted-line

我需要在editext字段中添加虚线。这是我到目前为止所尝试的。但没达到我的目标。它什么都没显示。为什么缺少虚线?我哪里出错?

在main.xml中提交的EditText

 <view
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        class="com.xxx.yyy.MainActivity$LinedEditText"
        android:background="@android:color/transparent"
        android:inputType="textEmailAddress"
        android:maxHeight="30dp"
        android:singleLine="true"
        android:minHeight="30dp"
        android:textSize="13dp" />

内部MainActivity ..

public static class LinedEditText extends EditText {
    private Rect mRect;
    private Paint mPaint;

    // we need this constructor for LayoutInflater
    public LinedEditText(Context context, AttributeSet attrs) {
        super(context, attrs);

        mRect = new Rect();
        mPaint = new Paint();

        mPaint.setARGB(255, 0, 0,0);
        mPaint.setStyle(Style.STROKE);
        mPaint.setPathEffect(new DashPathEffect(new float[] {1,2}, 0));
        mPaint.setColor(Color.RED);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int count = getLineCount();

        for (int i = 0; i < count; i++) {
            int baseline = getLineBounds(i, mRect);
            canvas.drawLine(mRect.left, baseline + 1, mRect.right, baseline + 1, mPaint);
        }

        super.onDraw(canvas);
    }
}

1 个答案:

答案 0 :(得分:-1)

将此添加到您的视图xml

android:ellipsize="end";

希望它有所帮助...