【问题标题】:How to change the hint color,underline color hint text,if edit text is empty and unfocused in android?如何更改提示颜色,下划线颜色提示文本,如果编辑文本为空且在android中没有焦点?
【发布时间】:2017-07-26 05:19:43
【问题描述】:

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

我想以编程方式更改提示颜色,未聚焦状态下的下划线颜色。

颜色.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>

【问题讨论】:

  • 使用 TextInputLayout
  • 着陆时的蓝色表示您在colors.xml中提到的颜色原色。发布您的styles.xml 和colors.xml
  • 请通过使用 styles.xml 和 colors.xml 发布来编辑您的问题
  • 我想以编程方式更改提示颜色,未聚焦状态下的下划线颜色。colors.xml #1976d2#E12929#CC1D1D#000000#1976d2#000000 #1976d2#666666

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


【解决方案1】:

你应该尝试一下

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

【讨论】:

    【解决方案2】:

    提示颜色

    android:textColorHint="@color/red"
    

    【讨论】:

    • 可以通过样式添加
    【解决方案3】:

    试试这个

    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;
            }
        });
    

    【讨论】:

    • 谢谢 Nilesh..但我也试过了..我想在未聚焦状态下更改提示颜色。但在 TextInputLayout 中,颜色仅在聚焦时发生变化。
    • @user8367573 check this link 一定会帮到你
    • @user8367573 比检查我提供的链接
    • 你需要创建自己的 OWN @user8367573
    • 在这个github代码中,下划线颜色消失了。当它没有焦点时
    【解决方案4】:

    试试下面这样的东西,要使编辑文本出错,请使用inputLayout.setError("Enther somehting");在验证您的条件后,您必须使用setErrorEnabled(false)删除

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

    【讨论】:

      猜你喜欢
      • 2016-02-02
      • 1970-01-01
      • 2015-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-11
      • 1970-01-01
      • 2023-03-31
      相关资源
      最近更新 更多