首页 文章

在android中更改edittext的背景颜色

提问于
浏览
26

如果我使用下面的代码更改了我的 EditText 的背景颜色,看起来该框缩小了,并且它不会保持默认 EditText 存在的蓝色底部边框的ICS主题 .

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#99000000"
    >
    <EditText
        android:id="@+id/id_nick_name"
        android:layout_marginTop="80dip"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#ffffff"  
    />
    <LinearLayout 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
             android:layout_marginTop="10dip"
             android:layout_marginLeft="20dip"
             android:layout_marginRight="20dip"
            android:orientation="horizontal"
            android:layout_below="@+id/id_nick_name">  
        <Button 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="add"
            android:layout_weight="1"
            />
         <Button 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="cancel"
            android:layout_weight="1"
            />
    </LinearLayout>
</RelativeLayout>

这是它的样子:

Image of EditText

9 回答

  • 1

    您应该做的是为edittext创建9补丁图像并将该图像设置为编辑文本背景 . 您可以使用this网站创建9个补丁

    我附上了一个样本9补丁图像供您参考 . 使用它作为编辑文本背景,你会得到一个想法 . 右键单击图像并选择“将图像另存为” . 保存图像时别忘了将其扩展名设为“9.png”

    enter image description here

  • 0

    一行惰性代码:

    mEditText.getBackground().setColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP);
    
  • 11

    这是最好的方法

    首先:在 res / drawable 中创建新的 xml 文件,将其命名为 rounded_edit_text 然后粘贴此:

    <?xml version="1.0" encoding="utf-8"?>
    <shape  xmlns:android="http://schemas.android.com/apk/res/android" 
        android:shape="rectangle" android:padding="10dp">
        <solid android:color="#F9966B" />
        <corners
            android:bottomRightRadius="15dp"
            android:bottomLeftRadius="15dp"
            android:topLeftRadius="15dp"
            android:topRightRadius="15dp" />
    </shape>
    

    第二:在res / layout复制和过去的代码中(代码为 EditText

    <EditText
        android:id="@+id/txtdoctor"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/rounded_edit_text"
        android:ems="10" >
        <requestFocus />
    </EditText>
    
  • 35

    我创建了color.xml文件,用于命名我的颜色名称(黑色,白色......)

    <?xml version="1.0" encoding="utf-8"?>
     <resources>
        <color name="white">#ffffff</color>
        <color name="black">#000000</color>
     </resources>
    

    在EditText中,设置颜色

    <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="asdsadasdasd"
            android:textColor="@color/black"
            android:background="@color/white"
            />
    

    或者在style.xml中使用样式:

    <style name="EditTextStyleWhite" parent="android:style/Widget.EditText">
        <item name="android:textColor">@color/black</item>
        <item name="android:background">@color/white</item>
    </style>
    

    并将ctreated样式添加到EditText:

    <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="asdsadasdasd"
            style="@style/EditTextStyleWhite"
            />
    
  • 9

    你正在使用的颜色是白色"#ffffff"是白色的,所以如果你想要的话,尝试在值中进行不同的改变,直到你从这个链接得到你的需要Color Codes它应该没问题

  • 1

    我发现最简单的解决方案是以编程方式更改背景颜色 . 这不需要处理任何9补丁图像:

    ((EditText) findViewById(R.id.id_nick_name)).getBackground()
        .setColorFilter(Color.<your-desi‌​red-color>, PorterDuff.Mode.MULTIPLY);
    

    资料来源:another answer

  • 0

    您应该使用样式而不是背景颜色 . 尝试搜索holoeverywhere然后我认为这将帮助您解决您的问题

    Using holoeverywhere

    只需更改一些9patch资源即可自定义edittext外观 .

  • 17

    对我来说这个代码是有用的所以把这个代码放在XML文件rounded_edit_text中

    <layer-list xmlns:android =“http://schemas.android.com/apk/res/android”> <item> <shape android:shape =“rectangle”> <stroke android:width =“1dp”android :color =“#3498db”/> <solid android:color =“#00FFFFFF”/> <padding android:left =“5dp”android:top =“5dp”android:right =“5dp”android:bottom =“5dp “> </ padding> </ shape> </ item> </ layer-list>

  • 0

    经过2天的努力,我找到了解决这个问题的工作方案,下面的解决方案非常适合那些只想更改少量编辑文本,通过java代码更改/切换颜色,并希望克服OS版本上不同行为问题的人由于使用了setColorFilter()方法 .

    import android.content.Context;
    import android.graphics.PorterDuff;
    import android.graphics.drawable.Drawable;
    import android.support.v4.content.ContextCompat;
    import android.support.v7.widget.AppCompatDrawableManager;
    import android.support.v7.widget.AppCompatEditText;
    import android.util.AttributeSet;
    import com.newco.cooltv.R;
    
    public class RqubeErrorEditText extends AppCompatEditText {
    
      private int errorUnderlineColor;
      private boolean isErrorStateEnabled;
      private boolean mHasReconstructedEditTextBackground;
    
      public RqubeErrorEditText(Context context) {
        super(context);
        initColors();
      }
    
      public RqubeErrorEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        initColors();
      }
    
      public RqubeErrorEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initColors();
      }
    
      private void initColors() {
        errorUnderlineColor = R.color.et_error_color_rule;
    
      }
    
      public void setErrorColor() {
        ensureBackgroundDrawableStateWorkaround();
        getBackground().setColorFilter(AppCompatDrawableManager.getPorterDuffColorFilter(
            ContextCompat.getColor(getContext(), errorUnderlineColor), PorterDuff.Mode.SRC_IN));
      }
    
      private void ensureBackgroundDrawableStateWorkaround() {
        final Drawable bg = getBackground();
        if (bg == null) {
          return;
        }
        if (!mHasReconstructedEditTextBackground) {
          // This is gross. There is an issue in the platform which affects container Drawables
          // where the first drawable retrieved from resources will propogate any changes
          // (like color filter) to all instances from the cache. We'll try to workaround it...
          final Drawable newBg = bg.getConstantState().newDrawable();
          //if (bg instanceof DrawableContainer) {
          //  // If we have a Drawable container, we can try and set it's constant state via
          //  // reflection from the new Drawable
          //  mHasReconstructedEditTextBackground =
          //      DrawableUtils.setContainerConstantState(
          //          (DrawableContainer) bg, newBg.getConstantState());
          //}
          if (!mHasReconstructedEditTextBackground) {
            // If we reach here then we just need to set a brand new instance of the Drawable
            // as the background. This has the unfortunate side-effect of wiping out any
            // user set padding, but I'd hope that use of custom padding on an EditText
            // is limited.
            setBackgroundDrawable(newBg);
            mHasReconstructedEditTextBackground = true;
          }
        }
      }
    
      public boolean isErrorStateEnabled() {
        return isErrorStateEnabled;
      }
    
      public void setErrorState(boolean isErrorStateEnabled) {
        this.isErrorStateEnabled = isErrorStateEnabled;
        if (isErrorStateEnabled) {
          setErrorColor();
          invalidate();
        } else {
          getBackground().mutate().clearColorFilter();
          invalidate();
        }
      }
    }
    

    在xml中使用

    <com.rqube.ui.widget.RqubeErrorEditText
                android:id="@+id/f_signup_et_referral_code"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_toEndOf="@+id/referral_iv"
                android:layout_toRightOf="@+id/referral_iv"
                android:ems="10"
                android:hint="@string/lbl_referral_code"
                android:imeOptions="actionNext"
                android:inputType="textEmailAddress"
                android:textSize="@dimen/text_size_sp_16"
                android:theme="@style/EditTextStyle"/>
    

    添加样式

    <style name="EditTextStyle" parent="android:Widget.EditText">
        <item name="android:textColor">@color/txt_color_change</item>
        <item name="android:textColorHint">@color/et_default_color_text</item>
        <item name="colorControlNormal">@color/et_default_color_rule</item>
        <item name="colorControlActivated">@color/et_engagged_color_rule</item>
      </style>
    

    用于切换颜色的java代码

    myRqubeEditText.setErrorState(true);
    myRqubeEditText.setErrorState(false);
    

相关问题