如果编辑文本是空的并且在android中没有聚焦,如何更改提示颜色,下划线颜色提示文本?

时间:2017-07-26 05:19:43

标签: android android-layout android-edittext android-textinputlayout textinputlayout

我有两个编辑文本。第一个是电子邮件,第二个是密码字段 单击登录按钮而不填写两个字段时,两个字段中的提示颜色和下划线更改为红色都是未聚焦状态。
着陆页上的未聚焦状态,颜色应为蓝色enter image description here 点击登录时不填充未聚焦状态enter image description here中的字段。

这是我的要求 我已经尝试了所有可能的方法来实现这种情况 但我无法控制专注和不专心的状态 请指导我解决这个问题。

<style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar"> <item name="windowNoTitle">true</item> <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="colorControlActivated">@color/primary</item> <item name="colorControlHighlight">@color/colorPrimary</item> </style>

我想更改提示颜色,以编程方式在未聚焦状态下强调颜色。

colors.xml

<resources> <color name="primary">#1976d2</color> <color name="primary_dark">#E12929</color> <color name="primary_darker">#CC1D1D</color> <color name="accent">#000000</color> <color name="blue">#1976d2</color> <color name="black">#000000</color> <color name="lightblue">#1976d2</color> <color name="lightgray">#666666</color> </resources>

4 个答案:

答案 0 :(得分:0)

像你这样的事情应该尝试

<android.support.design.widget.TextInputLayout
android:id="@+id/usernameWrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<EditText
    android:id="@+id/username"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textEmailAddress"
    android:hint="Username"/>

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

答案 1 :(得分:0)

试试这个

TextInputLayout emailInput, passInput;
EditText edtEmail, edtPass;
Button btnLogin;
String hintEmail = "",hintPass="";


emailInput = (TextInputLayout) findViewById(R.id.emailInput);
passInput = (TextInputLayout) findViewById(R.id.passInput);

edtEmail = (EditText) findViewById(R.id.edtEmail);
edtPass = (EditText) findViewById(R.id.edtPass);

btnLogin = (Button) findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(this);


    btnLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (edtEmail.getText().toString().trim().isEmpty()) {
                edtEmail.setHint("please enter Email Address ");
                hintEmail = "please enter Email Address ";
                edtEmail.setHintTextColor(ContextCompat.getColor(this,R.color.colorRed));
                emailInput.setHint("");

            }

            if (edtPass.getText().toString().trim().isEmpty()) {
                edtPass.setHint("please enter Password ");
                hintPass = "please enter Password  ";
                edtPass.setHintTextColor(getResources().getColor(R.color.colorRed));
                passInput.setHint("");

            }

        }
    });
    edtEmail.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            emailInput.setHint(hintEmail);
            edtEmail.setHint("");
            return false;
        }
    });
    edtPass.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            passInput.setHint(hintPass);
            edtPass.setHint("");
            return false;
        }
    });

答案 2 :(得分:0)

尝试下面这样的内容,对于编辑文本的错误,请在验证条件后使用inputLayout.setError("Enther somehting");,您必须使用setErrorEnabled(false)删除

  inputLayout_et = (TextInputLayout) findViewById(R.id.input_layout_newpassword);

  inputLayout_et.setErrorEnabled(false);

答案 3 :(得分:0)

提示颜色

android:textColorHint="@color/red"
相关问题