簡體   English   中英

Android中isEmpty時如何修復背景紅色TextInputLayout

[英]How to fix background red color TextInputLayout when isEmpty in Android

TextInputLayout isEmpty時我想要setError ,我寫了這段代碼但是當顯示錯誤消息時,為TextInputLayout設置紅色背景!

不想設置背景! 只想顯示錯誤消息。

在此處輸入圖片說明

我的代碼:

if (TextUtils.isEmpty(userName)) {
register_UserName_layout.setError("Insert Username");
}

XML 代碼:

<android.support.design.widget.TextInputLayout
    android:id="@+id/register_userUsernameTextLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/register_headerLayout"
    android:layout_margin="10dp"
    android:textColorHint="#c5c5c5">

    <EditText
        android:id="@+id/register_userUserNameText"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="@drawable/selector_bg_edit"
        android:hint="نام کاربری"
        android:paddingBottom="2dp"
        android:textColor="@color/colorAccent"
        android:textCursorDrawable="@drawable/bg_input_cursor"
        android:textSize="16sp" />
</android.support.design.widget.TextInputLayout>

我該如何解決這個問題? 謝謝大家 <3

我找到了解決此問題的方法。 您只需要創建一個自定義 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>

在我的情況下,我添加了行

<solid android:color="@color/transparent"/>

<shape android:shape="rectangle"
xmlns:android="http://schemas.android.com/apk/res/android" >
    <stroke android:color="@color/lightGray" android:width="1dp"/>
    <solid android:color="@color/transparent"/>
    <padding android:top="7dp" android:bottom="7dp" android:left="7dp" android:right="7dp"/>
    <corners android:radius="2dp"/>
</shape>

這只會導致紅色邊框而不是整個背景

您可以繼承 TextInputLayout 並使用它:

package com.mypackage;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputLayout;
import android.util.AttributeSet;

public class CustomTextInputLayout extends TextInputLayout {
    public CustomTextInputLayout(Context context) {
        super(context);
    }

    public CustomTextInputLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomTextInputLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void drawableStateChanged() {
        super.drawableStateChanged();
        clearEditTextColorfilter();
    }
    @Override
    public void setError(@Nullable CharSequence error) {
        super.setError(error);
        clearEditTextColorfilter();
    }

    private void clearEditTextColorfilter() {
        EditText editText = getEditText();
        if (editText != null) {
            Drawable background = editText.getBackground();
            if (background != null) {
                background.clearColorFilter();
            }
        }
    }
}

在您的布局中:

<com.mypackage.CustomTextInputLayout
    android:id="@+id/register_userUsernameTextLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/register_headerLayout"
    android:layout_margin="10dp"
    android:textColorHint="#c5c5c5">

    <EditText
        android:id="@+id/register_userUserNameText"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="@drawable/selector_bg_edit"
        android:hint="نام کاربری"
        android:paddingBottom="2dp"
        android:textColor="@color/colorAccent"
        android:textCursorDrawable="@drawable/bg_input_cursor"
        android:textSize="16sp" />
</android.support.design.widget.TextInputLayout>

這個問題的底線是檢查您的 EditText 是否設置了背景顏色。 如果是這樣,請將其刪除並在文本輸入布局小部件上添加背景顏色。 這將解決大紅框的問題。 至少對我來說是這樣。

子類 EditText 並應用以下方法:

覆蓋 getBackground 並創建一個帶有輸入背景的 Drawable

@Override
public Drawable getBackground() {
    return ContextCompat.getDrawable(getContext(), R.drawable.input_background);
}

你的 Drawable input_brackground 可以是這樣的:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid
        android:color="@color/inputBackgroundColor">
    </solid>
</shape>

並在 TextInputLayout 設置錯誤后應用 EditText 子類 setBackground ,例如:

private void showTheError(String error){
    textInputLayout.setError(error);
    editTextSubclassed.setBackgroundColor(getResources().getColor(R.color.inputBackgroundColor));
}

與 Shahin Mursalov 的回答類似,我在構造函數中調用方法init()以在創建視圖時存儲可繪制的背景。 此外,我還覆蓋了setBackgroundResource()方法來存儲背景,並從getBackground()返回它:

public class CustomEditText extends EditText{

    private Drawable backgroundDrawable;

    public EditTextContext context) {
        super(context);
        this.context = context;
    }

    public EditTextContext context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init(attrs);
    }

    public EditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        init(attrs);
    }


    public void init(AttributeSet attrs){
        TypedArray attributes = context.obtainStyledAttributes(attrs, new int[]{android.R.attr.background});
        this.backgroundDrawable = attributes.getDrawable(0);
        attributes.recycle();
    }

    @Override
    public void setBackgroundResource(@DrawableRes int resId) {
        this.backgroundDrawable = ContextCompat.getDrawable(context, resId);
        super.setBackgroundResource(resId);
    }

    @Override
    public Drawable getBackground() {
        if (backgroundDrawable != null){
            return backgroundDrawable;
        } else {
            return super.getBackground();
        }
    }
}

通過這種方式,我可以在布局中指定不同的背景並以編程方式更改它。

首先你的 EditText 背景 selector_bg_edit 應該是這樣的

 <selector xmlns:android="http://schemas.android.com/apk/res/android" >


    <item android:drawable="@drawable/code_view_item"
        android:state_focused="true"/>

    <item android:drawable="@drawable/sign_in_fields"
        android:state_focused="false"/>

    <item android:drawable="@drawable/sign_in_fields"
        android:state_active="false"/>

</selector>

最重要的是像這樣code_view_item.xml一樣把drawable放入透明顏色

<stroke android:width="1dp" android:color="#15A859" />
<solid android:color="#00FFFFFF" />
<corners android:radius="12dp"/>

對我有用的是將其放在我的自定義編輯文本類中的以下幾行:

override fun getBackground(): Drawable {
    return this.context.getDrawable(R.drawable.myDrawableResource)
}

我的解決方案:

yourEdit.background.clearColorFilter()
if (TextUtils.isEmpty(userName)) {
    register_UserName_layout.setError("Insert Username");
    txtInpit.setColorFilter(R.color.white);
}

暫無
暫無

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

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