编辑文本的聚焦提示栏颜色没有改变

The focused hint bar colour of edit text is not changing

想要更改聚焦提示 Edittext 颜色,但它没有改变。怎么了 ?已使用编辑文本主题中的所有属性,但尚未 changing.I 已将父主题从 MaterialComponents 更改为 App Compat,是因为那个吗?

这是我的 themes.xml 文件。

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.QuizApp" parent="Theme.AppCompat.DayNight.NoActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/purple_500</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:windowFullscreen">true</item>
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
        <!-- Customize your theme here. -->
        <item name="fontFamily">@font/font10</item>
    </style>

    <style name="heading_name" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
        <item name="fontFamily">@font/font1</item>
        <item name="textFillColor">#2B0031</item>
    </style>

    <style name="EditTextTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorControlNormal">@color/purple_gray</item>
        <item name="colorControlActivated">@color/purple_gray</item>
        <item name="colorControlHighlight">@color/purple_gray</item>
        <item name="hintTextColor">@color/purple_gray</item>
        <item name="android:textColorHint">@color/purple_gray</item>
        <item name="boxStrokeErrorColor">@color/purple_gray</item>
    </style>

    <style name="Theme.QuizApp.NoActionBarAndStatusBar" >
        <item name="windowActionBar">false</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowTranslucentStatus">true</item>
        <item name="android:windowTranslucentNavigation">true</item>
        <item name="android:fitsSystemWindows">true</item>
        <item name="fontFamily">@font/font10</item>
    </style>

    <style name="progressBarBlue" parent="@style/Theme.AppCompat">
        <item name="colorAccent">#4568dc</item>
    </style>

    <style name="Theme.QuizApp.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="Theme.QuizApp.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>

这是 activity_main.xml 文件。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_layout"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/screen_background"
    android:orientation="vertical"
    android:gravity="center"
    tools:context=".MainActivity">



    <TextView
        style="@style/heading_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:fontFamily="@font/font1"
        android:gravity="center"
        android:shadowColor="#450750"
        android:shadowDx="1.5"
        android:shadowDy="1.5"
        android:shadowRadius="1.6"
        android:text="Quiz App !"
        android:textColor="@color/white"
        android:textColorHighlight="#8F22A1"
        android:textColorHint="#7F1491"
        android:textSize="55sp"
        android:textStyle="bold" />

    <com.google.android.material.card.MaterialCardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        app:cardElevation="50dp"
        app:cardMaxElevation="100dp"
        app:cardUseCompatPadding="false"
        android:layout_marginEnd="20dp"
        app:cardCornerRadius="12dp"
        android:background="@color/white">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fontFamily="@font/font3"
                android:gravity="center_horizontal"
                android:text="Welcome ! "
                android:textColor="#000000"
                android:textSize="40sp"
                android:textStyle="bold" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Please Enter Your Name "
                android:textStyle="bold"
                android:textSize="20sp"
                android:gravity="center_horizontal"
                android:layout_marginTop="16dp"
                android:textColor="@color/black"/>
            
            <com.google.android.material.textfield.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                style="@style/Widget.Material3.TextInputEditText.OutlinedBox"
                android:layout_marginTop="20dp">


                <androidx.appcompat.widget.AppCompatEditText
                    android:id="@+id/et_name"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Name"
                    tools:text="Nandini"
                    android:textCursorDrawable="@drawable/cursor"
                    android:inputType="textCapWords"
                    android:textColor="#000000"
                    android:shadowColor="@color/purple_gray"
                    android:theme="@style/EditTextTheme"
                    android:textColorHighlight="@color/purple_gray"
                    android:textColorLink="@color/purple_gray"
                    android:backgroundTint="@color/purple_gray"
                    app:hintTextColor="@color/purple_gray"
                    android:outlineAmbientShadowColor="@color/purple_gray"
                    android:textColorHint="@color/purple_gray"/>
            </com.google.android.material.textfield.TextInputLayout>

            <Button
                android:id="@+id/btn_start"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:text="Start Quiz"
                android:textStyle="bold"
                android:elevation="100dp"
                android:stateListAnimator="@null"
                android:layout_gravity="center_horizontal"
                android:background="@drawable/button_bg"
                android:textColor="@color/white"
                android:textSize="20sp"/>

        </LinearLayout>

    </com.google.android.material.card.MaterialCardView>
    
</LinearLayout>

这是 MainActivity.kt 文件。

package com.nandini.android.quizapp


import android.content.Intent
import android.os.Bundle
import android.view.Window
import android.view.WindowManager
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity



class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {


        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_main)

        val btn_start : Button =findViewById(R.id.btn_start)
        val et_name : EditText=findViewById(R.id.et_name)
        btn_start.setOnClickListener {
            if(et_name.text.isEmpty())
            {
                et_name.setError("")
                Toast.makeText(this, "Enter name",Toast.LENGTH_LONG).show()
            }else{
                val intent = Intent(this,QuizQuestionActivity::class.java)
                intent.putExtra(Constants.USER_NAME,et_name.text.toString())
                startActivity(intent)
            }
        }


    }

}

首先,我将开始使用 Material 设计库以保持最新。

所以我会在我的 build.gradle(应用程序)中添加:

implementation 'com.google.android.material:material:1.5.0'

那么你的风格应该是这样的:

<style name="Theme.QuizApp" parent="Theme.MaterialComponents.DayNight">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/purple_500</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:windowFullscreen">true</item>
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
        <item name="textAppearanceSubtitle1">@style/TextAppearance.App.test</item>

        <!-- Customize your theme here. -->
    </style>

    <style name="TextAppearance.App.test" parent="TextAppearance.MaterialComponents.Subtitle1">
        <item name="hintTextColor">@color/purple_gray</item>
        <item name="colorControlNormal">@color/purple_gray</item>
        <item name="colorControlActivated">@color/purple_gray</item>
        <item name="colorControlHighlight">@color/purple_gray</item>
        <item name="android:textColorHint">@color/purple_gray</item>
        <item name="boxStrokeErrorColor">@color/purple_gray</item>
    </style>

不知道您的设计在 XML 中应该是什么样子,所以我只保留样式中的其余属性。

你的 MainActivity 应该变成:

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val btn_start: Button = findViewById(R.id.btn_start)
        val et_name: TextInputLayout = findViewById(R.id.et_name)

        btn_start.setOnClickListener {
            if (et_name.editText?.editableText.isNullOrEmpty()) {
                et_name.setError("")
                Toast.makeText(this, "Enter name", Toast.LENGTH_LONG).show()
            }else{
                val intent = Intent(this,QuizQuestionActivity::class.java)
                intent.putExtra(Constants.USER_NAME,et_name.text.toString())
                startActivity(intent)
            }
        }

    }
}

最后,在您的 activity 布局中,您将使用我们创建的样式(您必须在您的项目中进行调整):

<com.google.android.material.textfield.TextInputLayout
                android:id="@+id/et_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Default"
                style="@style/TextAppearance.App.test"
                android:layout_margin="10dp">

                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/default_edit_text"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textSize="20sp"
                     />

            </com.google.android.material.textfield.TextInputLayout>

让我知道它是否有效,在我的小测试项目中它看起来不错:)

我的小建议总的来说是:

  1. 不要使用 findViewById,因为如果您将来在 XML 中获得大量视图,这对应用程序的性能不利。我建议您使用 viewBinding 或 DataBinding。
  2. 尝试在代码之间添加相同数量的空格,使其看起来更简洁
  3. 尝试将你的字符串、维度提取到 xml 中,这样你就可以 re-use 它们在这里你可以找到一篇关于它的文章 https://suragch.medium.com/using-dimens-xml-in-android-10dec2fe485c and one for strings https://riptutorial.com/android/example/13829/working-with-strings-xml-file