如何在android中更改TextInputLayout颜色?

时间:2016-06-21 08:30:22

标签: android android-textinputlayout

您好我是Android的新手,在我的应用中,我使用的是TextInputLayout字段。

我设置了EditText边框颜色,我不想设置编辑文本背景颜色,但根据我的下面的代码,当我点击“SIGN UP”按钮时,edittext背景颜色显示为红色。

我们如何更改此背景颜色?

edittext_background: -

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

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

    <stroke
        android:width="1dip"
        android:color="@android:color/holo_purple" />

    <padding android:left="5dp"
        android:top="5dp"
        android:right="5dp"
        android:bottom="5dp"/>

</shape>

样式: -

 <style name="TextAppearance.App.TextInputLayout" parent="@android:style/TextAppearance">
        <item name="android:textColor">@color/splahbgcolor</item>
        <item name="android:textSize">14sp</item>
    </style>

main.xml中: -

<LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="?attr/actionBarSize"
        android:orientation="vertical"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:paddingTop="60dp">

        <android.support.design.widget.TextInputLayout
            android:id="@+id/input_layout_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:hintTextAppearance="@style/TextAppearance.App.TextInputLayout">

            <android.support.v7.widget.AppCompatEditText
                android:id="@+id/input_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:background="@drawable/edittext_background"
                android:ems="10"
                android:inputType="textEmailAddress"
                android:padding="8dp"
                android:textColor="@color/splahbgcolor"
                android:hint="@string/hint_name" />

        </android.support.design.widget.TextInputLayout>

MainActivity: -

public class MainActivity extends AppCompatActivity {

    private Toolbar toolbar;
    private EditText inputName, inputEmail, inputPassword;
    private TextInputLayout inputLayoutName, inputLayoutEmail, inputLayoutPassword;
    private Button btnSignUp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        inputLayoutName = (TextInputLayout) findViewById(R.id.input_layout_name);
        inputLayoutEmail = (TextInputLayout) findViewById(R.id.input_layout_email);
        inputLayoutPassword = (TextInputLayout) findViewById(R.id.input_layout_password);

        inputName = (EditText) findViewById(R.id.input_name);
        inputEmail = (EditText) findViewById(R.id.input_email);
        inputPassword = (EditText) findViewById(R.id.input_password);

        btnSignUp = (Button) findViewById(R.id.btn_signup);

        inputName.addTextChangedListener(new MyTextWatcher(inputName));
        inputEmail.addTextChangedListener(new MyTextWatcher(inputEmail));
        inputPassword.addTextChangedListener(new MyTextWatcher(inputPassword));

        btnSignUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                submitForm();
            }
        });
    }

    /**
     * Validating form
     */
    private void submitForm() {

        if (!validateName()) {
            return;
        }

        if (!validateEmail()) {
            return;
        }

        if (!validatePassword()) {
            return;
        }

        Toast.makeText(getApplicationContext(), "Thank You!", Toast.LENGTH_SHORT).show();
    }

    private boolean validateName() {

        if (inputName.getText().toString().trim().isEmpty()) {
            inputLayoutName.setError(getString(R.string.err_msg_name));
            requestFocus(inputName);
            return false;

        } else {
            inputLayoutName.setErrorEnabled(false);
        }

        return true;
    }

    private boolean validateEmail() {

        String email = inputEmail.getText().toString().trim();

        if (email.isEmpty() || !isValidEmail(email)) {
            inputLayoutEmail.setError(getString(R.string.err_msg_email));
            requestFocus(inputEmail);
            return false;
        } else {
            inputLayoutEmail.setErrorEnabled(false);
        }

        return true;
    }

    private boolean validatePassword() {

        if (inputPassword.getText().toString().trim().isEmpty()) {
            inputLayoutPassword.setError(getString(R.string.err_msg_password));
            requestFocus(inputPassword);
            return false;
        } else {
            inputLayoutPassword.setErrorEnabled(false);
        }

        return true;
    }

    private static boolean isValidEmail(String email) {

        return !TextUtils.isEmpty(email) && android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
    }

    private void requestFocus(View view) {

        if (view.requestFocus()) {
            getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        }
    }

    private class MyTextWatcher implements TextWatcher {

        private View view;

        private MyTextWatcher(View view) {
            this.view = view;
        }

        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        public void afterTextChanged(Editable editable) {

            switch (view.getId()) {

                case R.id.input_name:
                    validateName();
                    break;

                case R.id.input_email:
                    validateEmail();
                    break;

                case R.id.input_password:
                    validatePassword();
                    break;
            }
        }
    }
}

enter image description here

2 个答案:

答案 0 :(得分:0)

使用主题而不是hintTextAppearance

 <android.support.design.widget.TextInputLayout
            android:id="@+id/input_layout_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
           app:hintTextAppearance="@color/colorPrimary"
            android:theme="@style/TextAppearance>

你的风格

 <style name="TextAppearance" parent="@android:style/TextAppearance">
        <item name="android:textColor">@color/splahbgcolor</item>
        <item name="android:textColorHint">@color/splahbgcolor</item>
    </style>

答案 1 :(得分:0)

TL; DR版本:
每次致电inputName.setBackgroundResource(R.drawable.edittext_background);

后致电inputLayoutName.setError(getString(R.string.err_msg_name));

更长的版本:
发生这种情况是因为当您在TextInputlayout内部(在TextInputLayout的代码中)调用 setError(errormsg)后,在设置Errorview次调用方法之后使用此方法:     updateEditTextBackground();

在TextInputLayout类的updateEditTextBackground()方法中,您的editText的背景颜色使用以下行更改:

if (mErrorShown && mErrorView != null) {
        // Set a color filter of the error color
        editTextBackground.setColorFilter(
                AppCompatDrawableManager.getPorterDuffColorFilter(
                        mErrorView.getCurrentTextColor(), PorterDuff.Mode.SRC_IN));
    }

因此,您的Edittext的背景颜色会变为红色(这是从框架中Errorview's默认textAppearance样式中选取的。)

在您的情况下,只有可能的解决方法是在if子句中的所有setError方法之后添加以下行:

inputName.setBackgroundResource(R.drawable.edittext_background);

validateName()方法:

   private boolean validateName() {

    if (inputName.getText().toString().trim().isEmpty()) {
        inputLayoutName.setError(getString(R.string.err_msg_name));
        inputName.setBackgroundResource(R.drawable.edittext_background);
        requestFocus(inputName);
        return false;

    } else {
        inputLayoutName.setErrorEnabled(false);
    }

    return true;
}

目前可能只解决您的问题。
不确定这是否是处理自定义Edittext背景情况的适当方法。