简体   繁体   English

启用 EditText 下划线

[英]Enable EditText underline

By Default EditText has an underline.默认情况下EditText有一个下划线。 I want to start my program with the Edittext disabled and when someone clicks on an icon, the EditText is enabled.我想在禁用Edittext启动我的程序,当有人单击某个图标时,将启用EditText

Here is a picture showing the "to..." EditText not having an underline where the others do and the one with focus has it in the Accent color.这是一张图片,显示“to ...” EditText没有下划线,而其他人则这样做,而有焦点的则有强调色。

在此处输入图片说明

The code I have tried:我试过的代码:

foodIconButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            foodCurrentEditText.setEnabled(true);
            foodDesiredEditText.setEnabled(true);
            foodDesiredEditText.setFocusable(true);
            foodDesiredEditText.setBackgroundColor(Color.TRANSPARENT);
            //foodDesiredEditText.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#03DAC5")));
            //MyDrawableCompat.setColorFilter(foodDesiredEditText.getBackground(), ContextCompat.getColor(MainActivity.this, R.color.colorAccent));

            foodDesiredEditText.getBackground().setColorFilter(ContextCompat.getColor(MainActivity.this, R.color.colorAccent), PorterDuff.Mode.SRC_IN);
        }
    });

The bottom 3 lines of code are the different methods I have tried from suggestions in other questions on here.底部的 3 行代码是我从此处其他问题的建议中尝试过的不同方法。

If you want to hide and show the underline of the EditText , you can do it like the following:如果你想隐藏和显示EditText的下划线,你可以这样做:

To hide the line:要隐藏该行:

ColorStateList colorStateList = ColorStateList.valueOf(Color.TRANSPARENT);
ViewCompat.setBackgroundTintList(foodDesiredEditText, colorStateList);

To show the line:要显示该行:

ColorStateList colorStateList = ColorStateList.valueOf(ContextCompat.getColor(applicationContext, R.color.colorAccent));
ViewCompat.setBackgroundTintList(foodDesiredEditText, colorStateList);

A better solution if you are using androidx is:如果您使用androidx更好的解决方案是:

To hide the line:要隐藏该行:

foodDesiredEditText.setBackgroundResource(0);

To show the line:要显示该行:

foodDesiredEditText.setBackgroundResource(androidx.appcompat.R.drawable.abc_edit_text_material);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM