我在我的布局xml文件中创建一个EditText
但我想改变颜色线在EditText从Holo(例如)红色。 怎样才能做到呢?
我在我的布局xml文件中创建一个EditText
但我想改变颜色线在EditText从Holo(例如)红色。 怎样才能做到呢?
当前回答
使用这个方法..并根据视图名称修改它。这段代码工作得很好。
private boolean validateMobilenumber() {
if (mobilenumber.getText().toString().trim().isEmpty() || mobilenumber.getText().toString().length() < 10) {
input_layout_mobilenumber.setErrorEnabled(true);
input_layout_mobilenumber.setError(getString(R.string.err_msg_mobilenumber));
// requestFocus(mobilenumber);
return false;
} else {
input_layout_mobilenumber.setError(null);
input_layout_mobilenumber.setErrorEnabled(false);
mobilenumber.setBackground(mobilenumber.getBackground().getConstantState().newDrawable());
}
其他回答
更改Edittext的下划线颜色:
如果你想让整个应用程序共享这种风格,那么你可以这样做。
(1)转到styles.xml文件。你的AppTheme继承theme . appcompon . light . darkactionbar的父类(在我的例子中)将是你的应用程序中所有它们样式文件的基本父类。将它的名字改为“AppBaseTheme”。在它下面创建另一个样式,名称为AppTheme,并继承你刚才编辑的AppBaseTheme。它看起来如下所示:
<!-- Base application theme. -->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="windowActionBar">false</item>
<!--see http://www.google.com/design/spec/style/color.html#color-color-palette-->
<item name="colorPrimary">@color/material_brown_500</item>
<item name="colorPrimaryDark">@color/material_brown_700</item>
<item name="colorAccent">@color/flamingo</item>
<style name="AppTheme" parent="AppBaseTheme">
<!-- Customize your theme here. -->
</style>
然后将“colorAccent”更改为您希望EditText行颜色的任何颜色。
如果你有其他style.xml的值文件夹,这一步是非常重要的。因为这个文件将继承你之前的父xml文件。例如,我有values-19/styles.xml。这是专门为奇巧和以上。将它的父类更改为AppBaseTheme,并确保去掉“colorAccent”,这样它就不会覆盖父类的颜色。此外,您还需要保留特定于版本19的项。然后它会是这样的。
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:windowTranslucentStatus">true</item>
</style>
</resources>
我不喜欢之前的答案。最好的解决方案是使用:
<android.support.v7.widget.AppCompatEditText
app:backgroundTint="@color/blue_gray_light" />
android:backgroundTint for EditText仅适用于API21+。因此,我们必须使用支持库和AppCompatEditText。
注意:我们必须使用app:backgroundTint而不是android:backgroundTint
AndroidX版本
<androidx.appcompat.widget.AppCompatEditText
app:backgroundTint="@color/blue_gray_light" />
在XML布局中使用:
android:backgroundTint="@color/colorPrimary"
或者在Java代码中复制这个方法:
public void changeLineColorInEditText(EditText editText, int color) {
editText.setBackgroundTintList(ColorStateList.valueOf(color));
}
像这样使用它:
changeLineColorInEditText(editText, getResources().getColor(R.color.colorPrimary));
可拉的/ bg_edittext.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="@android:color/transparent" />
</shape>
</item>
<item
android:left="-2dp"
android:right="-2dp"
android:top="-2dp">
<shape>
<solid android:color="@android:color/transparent" />
<stroke
android:width="1dp"
android:color="@color/colorDivider" />
</shape>
</item>
</layer-list>
设置为EditText
<android.support.v7.widget.AppCompatEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_edittext"/>
对于低于21的API,你可以在EditText中使用theme属性 把下面的代码放入样式文件
<style name="MyEditTextTheme">
<item name="colorControlNormal">#FFFFFF</item>
<item name="colorControlActivated">#FFFFFF</item>
<item name="colorControlHighlight">#FFFFFF</item>
</style>
在EditText中使用此样式
<EditText
android:id="@+id/etPassword"
android:layout_width="match_parent"
android:layout_height="@dimen/user_input_field_height"
android:layout_marginTop="40dp"
android:hint="@string/password_hint"
android:theme="@style/MyEditTextTheme"
android:singleLine="true" />