![](/img/trans.png)
[英]How to fix background red color TextInputLayout when isEmpty in Android
[英]How to fix background red color in custom textinputlayout
發生錯誤時, Textinputlayout
背景在提供自定義背景以編輯文本后顯示為紅色。
我正在使用自定義 textinputlayout 放置app:hintEnabled="false"
<com.google.android.material.textfield.TextInputLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toRightOf="@+id/tvFirstNameSecondTest"
app:layout_constraintRight_toRightOf="parent"
android:id="@+id/tilLastNameSecondTest"
app:layout_constraintHorizontal_weight="1"
app:hintEnabled="false"
android:layout_marginTop="8dp"
android:layout_marginStart="@dimen/margin_4dp"
android:layout_marginEnd="@dimen/margin_16dp"
app:layout_constraintTop_toBottomOf="@+id/tvFirstNameThirdTest">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_form_filling"
android:id="@+id/etTilLastNameSecondTest"/>
</com.google.android.material.textfield.TextInputLayout>
我的 Kotlin 文件
class FarmerForm : AppCompatActivity() {
lateinit var context: Context
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_farmer_form)
setSupportActionBar(toolbar)
context = this
val updateProfileValidation = AwesomeValidation(ValidationStyle.TEXT_INPUT_LAYOUT)
updateProfileValidation.addValidation(
this,
R.id.tilFirstNameFirstTest,
RegexTemplate.NOT_EMPTY,
R.string.txt_enter_valid_name
)
btnRegister.setOnClickListener {
if(updateProfileValidation.validate()){
Toast.makeText(this, "Form Validated Successfully", Toast.LENGTH_LONG).show();
}else {
if(tilFirstNameFirstTest.error!=null){
etTilFirstNameFirstTest.background = resources.getDrawable(R.drawable.bg_form_error)
}
}
}
}
可繪制 XML 文件
<shape
xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/colorWhite"/>
<corners android:radius="@dimen/margin_4dp"/>
<stroke
android:width="2dp"
android:color="#ff0000"/>
<padding
android:left="8dp"
android:top="10dp"
android:bottom="10dp"
android:right="8dp"/>
如何修復此錯誤。
要更改填充文本字段的背景顏色,您可以:
TextInputLayout
上設置boxBackgroundColor
屬性textInputLayout.setBoxBackgroundColorResource()
和textInputLayout.setBoxBackgroundColor()
。 您還可以使用以下方法更改TextInputLayout
的筆畫:
textInputLayout.setBoxCornerRadii()
改變角半徑textInputLayout.setBoxStrokeColor()
改變描邊的顏色最后使用com.google.android.material.textfield.TextInputEditText
而不是EditText
。
要為 TextInput 字段設置背景顏色,請使用正確的 TextInputLayout 格式,而不是下面的代碼,
<com.google.android.material.textfield.TextInputLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toRightOf="@+id/tvFirstNameSecondTest"
app:layout_constraintRight_toRightOf="parent"
android:id="@+id/tilLastNameSecondTest"
app:layout_constraintHorizontal_weight="1"
app:hintEnabled="false"
android:layout_marginTop="8dp"
android:layout_marginStart="@dimen/margin_4dp"
android:layout_marginEnd="@dimen/margin_16dp"
app:layout_constraintTop_toBottomOf="@+id/tvFirstNameThirdTest">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_form_filling"
android:id="@+id/etTilLastNameSecondTest"/>
用這個:
<com.google.android.material.textfield.TextInputLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toRightOf="@+id/tvFirstNameSecondTest"
app:layout_constraintRight_toRightOf="parent"
android:id="@+id/tilLastNameSecondTest"
**app:boxBackgroundColor="@color/green"**
app:layout_constraintHorizontal_weight="1"
app:hintEnabled="false"
android:layout_marginTop="8dp"
android:layout_marginStart="@dimen/margin_4dp"
android:layout_marginEnd="@dimen/margin_16dp"
app:layout_constraintTop_toBottomOf="@+id/tvFirstNameThirdTest">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/etTilLastNameSecondTest"/>
這些更改是:將 EditText 替換為“ com.google.android.material.textfield.TextInputEditText ”並添加“ app:boxBackgroundColor="@color/red ”。 這將消除您遇到的錯誤,您還可以實現您希望 InputBox 具有的樣式
您可以在活動主題樣式中添加editTextBackground
顏色,例如
styles.xml文件
<style name="BaseTheme_FullScreen2" parent="Theme.AppCompat.Light.NoActionBar">
<item name="windowActionBar">false</item>
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:textColorHint">@color/white</item>
<item name="editTextBackground">@color/yellow</item>
<item name="actionBarSize">48dp</item>
<item name="android:actionBarSize">56dp</item>
<item name="android:windowContentOverlay">@null</item>
<item name="colorControlNormal">@color/bg_color</item>
<item name="colorControlActivated">@color/white</item>
<item name="android:actionMenuTextAppearance">@style/AppTheme.PopupOverlay</item>
</style>
AndroidManifest.xml
<activity
android:name=".modules.mainModule.homeScreen.HomeActivity"
android:label="@string/title_activity_home"
android:theme="@style/BaseTheme_FullScreen2"
android:windowSoftInputMode="adjustResize|adjustPan"></activity>
對於自定義 textinputlayout,您只需要創建一個自定義 EditText 並覆蓋它的 getBackground() 方法即可返回一個新的可繪制對象。 這樣,TextInputLayout 將無法在 EditText 的背景上設置顏色過濾器,因為您不返回 EditText 的背景,而是返回另一個可繪制對象。 見下文:
@Override
public Drawable getBackground() {
return ContextCompat.getDrawable(getContext(), R.drawable.some_drawable);
}
並使用 TextInputLayout 中的自定義 EditText:
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CustomEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/custom_edit_text_bg" />
</android.support.design.widget.TextInputLayout>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.