首页 文章

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

提问于
浏览
-5

我有两个编辑文本 . 第一个是电子邮件,第二个是密码字段 .
当我单击登录按钮而不填写两个字段时,两个字段中的提示颜色和下划线变为红色都是未聚焦状态 .
在着陆页上未聚焦状态,颜色应为蓝色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

    像你这样的东西应该尝试

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

    hint color

    android:textColorHint="@color/red"
    
  • 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;
            }
        });
    
  • 0

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

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

相关问题