簡體   English   中英

Android:激活時更改材質 TextInputLayout 的背景顏色

[英]Android: Change background color of Material TextInputLayout when active

我正在嘗試在 Material TextInputLayout 處於活動狀態時更改其背景顏色。 當我單擊 textinputedittext 時,它變得透明,我需要設置背景顏色。 我正在附加打印屏幕。

非常感謝

  1. 第一視角

  2. 具有透明顏色的活動文本輸入 - 需要保持相同的藍色背景不透明

  3. 鍵入文本時文本輸入背景顏色正確

     <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>

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?) {
        }
    })

使用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

   }
  });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM