繁体   English   中英

如何在Android中更改EditText的底线颜色?

[英]How to change bottom line color of EditText in Android?

我尝试过多种方式更改我的编辑文本底部颜色,但它没有改变。 我不想使用drawable。

这是我的xml和我尝试过的方法。

方法一:

android:backgroundTint="@color/colorPrimary"

方法二:

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>

    <item name="colorControlNormal">@color/colorPrimary</item>
    <item name="colorControlActivated">@color/colorPrimary</item>
    <item name="colorControlHighlight">@color/colorPrimary</item>

</style>

</resources>

这是我的xml:

 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res /android"
 xmlns:tools="http://schemas.android.com/tools"
 xmlns:card_view="http://schemas.android.com/apk/res-auto"
 android:id="@+id/b"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context="com.example.isaac.loginexamples.b">

<RelativeLayout
    android:background="#009688"
    android:layout_width="match_parent"
    android:layout_height="@dimen/relative_half_screen_height">
</RelativeLayout>

<android.support.v7.widget.CardView
    card_view:cardElevation="18dp"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:layout_width="@dimen/card_view_width"
    android:layout_height="@dimen/card_view_height">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <View
            android:id="@+id/header_logo_two"
            android:background="#22313F"
            android:layout_width="match_parent"
            android:layout_height="@dimen/view_header_height"></View>

        <View
            android:background="@drawable/shadow"
            android:layout_below="@+id/header_logo_two"
            android:layout_width="match_parent"
            android:layout_height="7dp"></View>

        <ImageView
            android:layout_marginTop="25dp"
            android:layout_centerHorizontal="true"
            android:id="@+id/logo_two"
            android:background="@mipmap/ic_launcher"
            android:layout_width="@dimen/logo_image_width_height"
            android:layout_height="@dimen/logo_image_width_height" />

        <EditText
            android:backgroundTint="@color/colorPrimary"
            android:id="@+id/username"
            android:layout_marginTop="15dp"
            android:layout_centerHorizontal="true"
            android:layout_below="@+id/logo_two"
            android:layout_width="270dp"
            android:layout_height="45dp"
            android:inputType="textEmailAddress" />

        <EditText
            android:layout_marginTop="8dp"
            android:layout_alignLeft="@+id/username"
            android:layout_below="@+id/username"
            android:layout_width="240dp"
            android:layout_height="45dp" />

    </RelativeLayout>

    </android.support.v7.widget.CardView>

    </RelativeLayout>

我已经尝试过我在网上看过的那两种方法,但它不起作用。 我正在运行最新版本的Android Studio。 我希望有一个解决方案。 注意:我不想使用可绘制的。 非常感谢你 !

如果您不想使用drawable,可以使用edittext下面的“View”,并根据edittext的focuschange更改该View的颜色

<View
    android:id="@+id/username_view"
    android:layout_width="match_parent"
    android:layout_height="@dimen/size_1"
    android:layout_alignEnd="@+id/username"
    android:layout_alignLeft="@+id/username"
    android:layout_alignRight="@+id/username"
    android:layout_alignStart="@+id/username"
    android:layout_below="@+id/username"
    android:background="@color/grey"/>

在您的活动或片段中,添加这些代码行,负责根据用户名焦点更改来更改视图的颜色

@OnFocusChange(R.id.username)
void onUserNameFocusChanged(View v, boolean hasFocus) {
    if (userNameView != null) {
        int focusViewColor = hasFocus ? getResources().getColor(R.color.red) : getResources().getColor(R.color.grey);
        userNameView.setBackgroundColor(focusViewColor);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM