1

我需要在我的编辑文本字段中有虚线。这是我到目前为止所尝试的。但是没有达到我的目的。它什么也不显示。为什么虚线不见了?我哪里错了?

在 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);
    }
}
4

1 回答 1

-1

将此添加到您的视图 xml

android:ellipsize="end";

希望能帮助到你...

于 2012-09-24T09:32:51.310 回答