Android添加边框以编程方式编辑文本

时间:2013-10-31 12:16:11

标签: android android-edittext background-color

我使用了this示例并尝试以编程方式将其添加到我的编辑文本中,如editText.setBackgroundResource(R.drawable.edit_text_back);,但它不起作用。我怎么能做到这一点?有什么建议或想法吗?

编辑 editText也是以编程方式定义的。

EditText editText = new EditText(this.getApplicationContext());

我将其添加到表格行

受审

editText.setBackground(getResources().getDrawable(R.drawable.edit_text_back));
editText.setBackgroundDrawable(getResources().getDrawable(R.drawable.edit_text_back));

编辑文字创建

TableRow row = (TableRow) findViewById(R.id.table_row_kind);
TableRow.LayoutParams rowP = new TableRow.LayoutParams();
        rowP.setMargins(10, 0, 0, 0);
editText = new EditText(this.getApplicationContext());
editText .setGravity(Gravity.FILL_HORIZONTAL);
editText .setLayoutParams(rowP);
editText .setFilters(new InputFilter[]{txtFilter});
editText.setBackground(getResources().getDrawable(R.drawable.edit_text_back));

row.xml

<TableRow
    android:id="@+id/table_row_kind"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="5dip" >

    <TextView
       android:layout_width="250sp"
       android:text="Kind"
       android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>

7 个答案:

答案 0 :(得分:17)

我也有同样的问题,我通过以下方式解决。它是一个xml文件放在你的drawable文件夹中,并将这个xml设置为EditText的背景

活动代码:

EditText foo = (EditText)findViewById(R.id.editText);
foo.setBackgroundResource(R.drawable.backtext);

backtext.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
       <solid android:color="#ffffff" />
       <stroke android:width="1dip" android:color="#000000"/>
    </shape>

答案 1 :(得分:10)

将edittext.xml文件创建为可绘制文件夹

<?xml version="1.0" encoding="utf-8"?>
<!--  res/drawable/rounded_edittext.xml -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<solid android:color="#FFFFFF"/>
<stroke
    android:width="1dp"
    android:color="@android:color/black" />
<corners
 android:bottomRightRadius="15dp"
 android:bottomLeftRadius="15dp"
 android:topLeftRadius="15dp"
android:topRightRadius="15dp"/>
</shape>


in your main.xml
<EditText
background="drawable/edittext.xml"
/>

答案 2 :(得分:3)

此代码适用于以编程方式绘制任何视图的边框

package com.example.border;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.ShapeDrawable;

public class ShapeDrawableWithoutBottom extends ShapeDrawable {
    private float mLineWidth = 1f;
    private final Paint mLinePaint;
    private int color;

    public ShapeDrawableWithoutBottom() {

        // No color specified, so call constructor with default color White
        this(Color.WHITE);
    }

    public ShapeDrawableWithoutBottom(int layoutColor) {

        // use the setter defined below, to set the main color for this drawable
        // setColor(color);
        setColor(layoutColor);
        // setup the Paint for drawing the lines
        mLinePaint = new Paint();
        mLinePaint.setStyle(Paint.Style.STROKE);
        mLinePaint.setStrokeWidth(mLineWidth);
    }

    public void setColor(int color) {
        Paint paint = getPaint();
        paint.setColor(color);

    }

    public void setLineColor(int color) {
        this.color = color;
    }

    public void setLineWidth(float lineWidth) {
        mLineWidth = lineWidth;
        mLinePaint.setStrokeWidth(mLineWidth);
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);

        // bottom black line
        // //////////////////

        mLinePaint.setColor(Color.parseColor("#00000000"));
        mLinePaint.setAlpha((int) (255 * 0.0)); // Opacity 90%
        canvas.drawLine(getBounds().left, getBounds().bottom - mLineWidth
                * 0.5f, getBounds().right, getBounds().bottom - mLineWidth
                * 0.5f, mLinePaint);

        // translucent grey rim
        // /////////////////////

        mLinePaint.setColor(color);
        mLinePaint.setAlpha((int) (255 * 0.7)); // Opacity 70%

        // top
        canvas.drawLine(getBounds().left, getBounds().top + mLineWidth * 0.5f,
                getBounds().right, getBounds().top + mLineWidth * 0.5f,
                mLinePaint);

        // left
        canvas.drawLine(getBounds().left + mLineWidth * 0.5f,
                getBounds().bottom , getBounds().left + mLineWidth
                        * 0.5f, getBounds().top + mLineWidth, mLinePaint);

        // right
        canvas.drawLine(getBounds().right - mLineWidth * 0.5f,
                getBounds().bottom , getBounds().right - mLineWidth
                        * 0.5f, getBounds().top + mLineWidth, mLinePaint);

        // top white line
        // ///////////////

        mLinePaint.setColor(Color.WHITE);
        mLinePaint.setAlpha((int) (255 * 0.5)); // Opacity 50%
        canvas.drawLine(getBounds().left + mLineWidth, getBounds().top
                + mLineWidth * 1.5f, getBounds().right - mLineWidth,
                getBounds().top + mLineWidth * 1.5f, mLinePaint);
    }
}

答案 3 :(得分:1)

试试这个,我动态添加了edittext然后设置它的背景,它的工作原理。

 LinearLayout layout=(LinearLayout)findViewById(R.id.layout);
                    EditText edit=new EditText(MainActivity.this);
                    edit.setBackgroundResource(R.drawable.abc);
                    edit.setMaxWidth(100);
                    edit.setMinHeight(100);
                    edit.setText("hello");
                    layout.addView(edit);

答案 4 :(得分:0)

这是您可以为编辑文本创建边框。将其保存为res / drawable

<?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
        <item>
         <shape android:shape="rectangle">
         <gradient android:startColor="#f9f9f9"
                android:centerColor="#ffffff"
                android:endColor="#ffffff"
                android:angle="90"/>
         <stroke  android:width="1dp" android:color="#2B547E"/>
        </shape>
         </item>
    </layer-list>

答案 5 :(得分:0)

使用

 editText.setBackground(getResources().getDrawable(R.drawable.edit_text_back));

已成为

editText.setBackgroundResource(R.drawable.edit_text_back);

答案 6 :(得分:0)

我知道这太晚了。但是要非常轻松地实现这一目标,只需使用Material组件。

  1. 您将需要使用Material组件作为您的应用主题:

    <resources>
       <style name="MyAppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
          <!-- Define your theme color values here. colorPrimary etc. -->
       </style>
    </resources>
    
  2. 在styles.xml文件中定义您的EditText样式:

    <style name="MyOutlinedEditText  parent="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"> . 
       <!-- Define your edit text style options here. textSize, color, stroke width and color -->
    </style>
    

用法(以xml格式):

<EditText
    style="@style/MyOutlinedEditText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

以编程方式应用主题需要更多的努力。如果需要的话,我也会解释一下。就是说,真正不需要以编程方式创建editText(这是我的个人看法)。