【问题标题】:Android add border to edit text programmaticallyAndroid添加边框以编程方式编辑文本
【发布时间】:2013-11-11 10:42:54
【问题描述】:

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

EDIT 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));

行.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>

【问题讨论】:

  • editText.setBackgroundDrawable(getResources().getDrawable(R.drawable.edit_text_back));试试这个@Lunchbox
  • 我认为这个帖子可能会对你有所帮助。 stackoverflow.com/questions/3496269/…
  • 您链接的示例SO问题解释得非常丰富..
  • @BojanJovanovic 我尝试了你的答案,但仍然没有结果,我没有扩展 TextView,而是扩展了 EditText。不过谢谢
  • @Indiandroid 我试过但没用,请看我的编辑

标签: android android-edittext background-color


【解决方案1】:

嗯,我也有同样的问题,我通过以下方式解决。它是一个 xml 文件,将其放在您的可绘制文件夹中,并将此 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>

【讨论】:

  • 嗨,它不起作用,可能是因为我以编程方式创建了编辑文本。查看我的编辑
  • @simmant 这是一个很好的答案,但它仅适用于在 xml 中定义的编辑文本。这就是我的问题所在
【解决方案2】:

在drawable文件夹中创建一个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"
/>

【讨论】:

  • 这适用于在 xml 中定义的编辑文本,但我的不是
  • 你想在你动态创建的edittext上设置bg边框,对吧?
  • 是的,只是黑色,一旦它起作用,我会将其更改为正确的颜色
  • 这不起作用:我从标准的ui元素中派生了许多ui元素。所以我没有边框。我的 TextviewExt,它继承自 Textview 没有边框。我以编程方式设置了 textviewext 的背景色以及文本和文本颜色。形状方法做了一些事情,但它似乎也不能正常工作。我必须覆盖我的 TextViewExt 的 onDraw 并且在此我可以调用 setBackgroundRessource。这在我的派生中没有其他地方起作用。似乎,好像某种调用者必须修改孩子,而不是孩子本身。另外,它给了我两种颜色.我只需要一个黑色边框。现在怎么办?
  • @icbytes 你的问题解决了吗?我只是最终将我的 EditText 添加到了 LinearLayout 中。比似乎成功了。 TableRow 是问题所在,我仍然不知道为什么
【解决方案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);
    }
}

【讨论】:

  • 代码看起来不错,但您不必将 Paint 对象设置为 final,因为您在第二个构造函数中再次实例化它
【解决方案4】:

试试这个,我动态添加了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);

【讨论】:

  • 嗨,谢谢,但它仍然不起作用。我将编辑文本添加到 TableRow 而不是线性布局。添加了 setMaxWidth 和 setMinHeight 但没有效果。
【解决方案5】:

使用

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

设置

editText.setBackgroundResource(R.drawable.edit_text_back);

【讨论】:

  • 嗨,对不起,这是不行的。我刚才又试了一次,结果还是一样,只是我的编辑文本没有任何边框。
  • @Lunchbox 我刚刚在我的 Galaxy Note-3 上试过。似乎有效。
  • edtLogin.setBackground(getResources().getDrawable(R.drawable.test));
  • 我编辑了我的答案,这就是我没有在 xml 中定义我的编辑文本的意思,如果我造成任何混乱,抱歉
【解决方案6】:

这是您可以为编辑文本制作边框。将其保存到 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>

【讨论】:

  • 什么是? xml 保存到我的 res/drawable
  • 奇怪,几分钟后再试?
  • schemas.android.com/apk/res/android" >
  • 只需将file_name.xml复制并保存到res/drawable中
  • 嗨,我试过这个,它确实适用于 xml 中存在的编辑文本,但我的没有,因为它是在 java 中创建的。感谢您的努力
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多