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

问题描述:

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

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

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

使用TextInputLayout – MinnuKaAnae

+0

这时候登陆意味着它是你的colorprimary您在colors.xml提到蓝色。发布您的styles.xml和colors.xml – MinnuKaAnae

+0

user8367573

像你这样的东西应该尝试

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

试试这个

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

感谢Nilesh ..但我也尝试过..我想改变提示颜色在未聚焦的状态。但在TextInputLayout中,颜色只在聚焦时才会改变。 – user8367573

+0

@ user8367573 [检查此链接](https://*.com/questions/38327430/textinputlayout-different-color-for-hint-label-when-not-focused)它肯定会帮助你 –

+0

我需要自定义编辑文本。 – user8367573

尝试下面这样的事情,对于做出错误验证后EDITTEXT使用inputLayout.setError("Enther somehting");你必须使用setErrorEnabled(false)

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

    inputLayout_et.setErrorEnabled(false); 

一丝色彩

android:textColorHint="@color/red" 
+0

这不提供问题的答案。要批评或要求作者澄清,请在其帖子下方留言。 - [来自评论](/ review/low-quality-posts/16830591) –

+0

是Mr.ric..解决方案是什么? – user8367573

+0

你可以通过样式添加它 –