-4

我需要在未聚焦时动态更改提示颜色属性。是否可以动态更改提示颜色(不在聚焦状态)。

实际设计

在此处输入图像描述

和我的 xml

在此处输入图像描述

未聚焦时,我无法以编程方式更改提示颜色属性。我被困住了。有可能吗?请帮我。这是我的代码 -

         btnSignUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final String email = emailEditText.getText().toString();
                final String pass = passEditText.getText().toString();
                if (email.isEmpty()) {
                    inputLayoutEmail.setHint("email is required to login");
                    inputLayoutEmail.setHintTextAppearance(R.style.EditTextLayout);
                    inputLayoutEmail.setError(" ");
                    emailEditText.setHintTextColor(getResources().getColor(R.color.primary_dark));

                }
                if (pass.isEmpty()) {
                    inputLayoutPassword.setHint("password is required to login");
                   passEditText.setHintTextColor(getResources().getColor(R.color.primary_dark));

                    inputLayoutPassword.setHintTextAppearance(R.style.EditTextLayout);
                    inputLayoutPassword.setError(" ");
                    inputLayoutPassword.clearFocus();
                    passEditText.setHint("Password is required to login");
                  passEditText.setHintTextColor(Color.RED);
                }
4

3 回答 3

0

请尝试此代码。当您单击登录并且字段为空时,它将提示文本颜色更改为红色。当您单击以在edittext中写入任何内容时,它会将提示文本颜色重新更改为黑色。希望有帮助..

这是我的活动 -

public class Main2Activity extends AppCompatActivity implements View.OnFocusChangeListener{

    EditText emailEditText, passEditText;
    Button btnSignUp;

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

        emailEditText = (EditText) findViewById(R.id.edt1);
        passEditText = (EditText) findViewById(R.id.edt2);
        btnSignUp = (Button) findViewById(R.id.btnSignUp);

        emailEditText.setOnFocusChangeListener(this);
        passEditText.setOnFocusChangeListener(this);
        btnSignUp.setOnFocusChangeListener(this);

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


                final String email = emailEditText.getText().toString();
                final String pass = passEditText.getText().toString();
                if (email.isEmpty()) {

                    emailEditText.setHintTextColor(Color.RED);

                }
                if (pass.isEmpty()) {

                    passEditText.setHintTextColor(Color.RED);

                    passEditText.setHint("Password is required to login");
                    passEditText.setHintTextColor(Color.RED);
                }
            }
        });
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
            if(v instanceof EditText)
            {
                ((EditText)v).setHintTextColor(Color.BLACK);
                v.requestLayout();
                v.refreshDrawableState();

            }
    }
}

还有我的 XML -

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:id="@+id/edt1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="PLease enter Id"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:textColor="@color/black"/>

    <EditText
        android:id="@+id/edt2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="PLease enter Password"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:textColor="@color/black" />


    <Button
        android:id="@+id/btnSignUp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Login"
        />
</LinearLayout>
于 2017-08-10T06:48:17.717 回答
0
    editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean b) {
            if (!b) {
         editText.setHintTextColor(getResources().getColor(R.color.red));
            } else {
             // set which color you want when edittext is focused
        }
    });

或者您也可以使用主题来设置颜色

<style name="InputLayoutTheme" parent="Widget.Design.TextInputLayout">
     // Whenever input layout is un-focused, below is the color for label 
    <item name="android:textColorHint">@color/dark_gray</item>
    // Whenever input layout is un-focused, below is the underline color 
    <item name="colorControlNormal">@color/dark_gray</item>
    <!-- Whenever input layout is focused, below is the color for label,    cursor & underline-->
    <item name="colorControlActivated">@color/button_text_blue2</item>
</style>
于 2017-07-28T04:54:50.340 回答
0

使用此代码可能会对您有所帮助。

 button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (editText1.getText().toString().length() == 0) {
                    editText1.setHintTextColor(Color.RED);
                }
                if (editText2.getText().toString().length() == 0) {
                    editText2.setHintTextColor(Color.RED);
                }
            }
        });
于 2017-07-28T04:36:19.947 回答