单行自定义edittext背景

时间:2014-01-17 12:19:17

标签: android android-xml

有人能告诉我如何创建edittext,看起来像这样吗?

enter image description here

基本上我想要的是用一行替换edittext的文本框!这可能吗?

2 个答案:

答案 0 :(得分:2)

您可以拥有drawable underline,然后将其放入xml:

android:background="@drawable/edit_text_underline"

这不是一个优雅的解决方案,但它是一个解决方案。

这是图片:

enter image description here

此外,您可以使用.9 image。在这种情况下,请不要忘记设置padding = 0。 抱歉我的英语很糟糕。

答案 1 :(得分:1)

试试这段代码:

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

    public LinedEditText(Context context, AttributeSet attrs) 
    {
        super(context, attrs);
        mRect = new Rect();
        mPaint = new Paint();
        // define the style of line
        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        // define the color of line
        mPaint.setColor(Color.BLACK);
        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.what_wirite_bg);

    }

    @Override
    protected void onDraw(Canvas canvas) 
    {
        int height = getHeight();
        int lHeight = getLineHeight();
        // the number of line
        int count = height / lHeight;
        if (getLineCount() > count) 
        {
            // for long text with scrolling
            count = getLineCount();
        }
        Rect r = mRect;
        Paint paint = mPaint;
        setPadding(10, 0, 10, 15);

        // first line
        int baseline = getLineBounds(0, r);
        //setCompoundDrawablePadding(5);
        if(HomeActivity.height>640)
        {
            //setPadding(0, 15, 0, 0);
            setLineSpacing(25.0f, 1.0f);
        }
        else
        {
            setLineSpacing(12.0f, 1.0f);
        }
        //setLineSpacing(20.0f, 0.5f);
        //setPadding(0, 15, 0, 0);
        /*setCompoundDrawablePadding(5);
        setLineSpacing(15.0f, 0.8f); ///////////////////Spacing change
        setPadding(0, 15, 0, 0); */ ///////////////////padding change
        // draw the remaining lines.
        for (int i = 0; i < count; i++) 
        {
            //canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint);
            if(HomeActivity.height>640)
            {
                canvas.drawBitmap(bitmap, r.left, baseline + 16.0f, paint); //////////////To be Change
            }
            else
            {
                canvas.drawBitmap(bitmap, r.left, baseline + 5.5f, paint); //////////////To be Change
            }

            //canvas.drawBitmap(bitmap, r.left, baseline + 5.5f, paint); //////////////To be Change
            // next line
            baseline += (getLineHeight());
        }
        super.onDraw(canvas);
    }

     @Override
        public InputConnection onCreateInputConnection(EditorInfo outAttrs)
        {
            InputConnection conn = super.onCreateInputConnection(outAttrs);
            outAttrs.imeOptions &= ~EditorInfo.IME_FLAG_NO_ENTER_ACTION;
            return conn;
        }

    @Override
    public Editable getText() {
        // TODO Auto-generated method stub
        return super.getText();
    }

在xml布局中使用此EditText。使用这个你的ediText有你想要的背景..