Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
605 views
in Technique[技术] by (71.8m points)

xaml - Android: Change background color of Material TextInputLayout when active

I am trying to change background color of Material TextInputLayout when its active. When I click the textinputedittext it becomes transparent and I need to set the background color. I am attaching printscreens.

Many thanks

  1. first view

  2. active text input with transparent color -need to keep same blue background not transparent

  3. text input background color is correct when typing text

     <com.google.android.material.textfield.TextInputLayout
         android:id="@+id/etPasswordLayout"
         style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_marginLeft="24dp"
         android:layout_marginTop="24dp"
         android:layout_marginRight="24dp"
         android:hint="Enter your password"
         app:endIconTint="@null"
         app:endIconMode="password_toggle"
         android:textColorHint="@color/darkBlue"
         app:endIconDrawable="@drawable/ic_password_visibility"
         app:shapeAppearance="@style/Rounded">
    
         <com.google.android.material.textfield.TextInputEditText
             android:id="@+id/etPassword"
             style="@style/Text.NormalText"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:background="@drawable/round_corner_toggle"
             android:inputType="textPassword"
             android:minHeight="60dp"
             android:text=""
             android:textColor="@color/darkBlue" />
    
     </com.google.android.material.textfield.TextInputLayout>
    

Implementation of textchangedlistener:

 val layoutPassword = findViewById<TextInputLayout>(R.id.layout_Password)
    val inputPassword = findViewById<TextInputEditText>(R.id.input_Password)
    inputPassword.addTextChangedListener(object: TextWatcher {

        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            layoutPassword.setBackgroundColor(Color.parseColor("#D0DDFF"))
            inputPassword.setBackgroundColor(Color.parseColor("#D0DDFF"))
        }

        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            layoutPassword.setBackgroundColor(Color.parseColor("#D0DDFF"))
             inputPassword.setBackgroundColor(Color.parseColor("#D0DDFF"))
        }

        override fun afterTextChanged(s: Editable?) {
        }
    })

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

use addTextChangedListener

field1 = (TextInputLayout)findViewById(R.id.field1);
field2 = (TextInputEditText)findViewById(R.id.field2);

field2.addTextChangedListener(new TextWatcher() {

   public void afterTextChanged(Editable s) {}

   public void beforeTextChanged(CharSequence s, int start,
                                    int count, int after) {

      // before text change 
      field1.setBackgroundColor() // set your color before active

   }

   public void onTextChanged(CharSequence s, int start,
                                 int before, int count) {

      // after add text in TextInputEditText
      field1.setBackgroundColor() // set your color after active

   }
  });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...