启用EditText下划线

huangapple go评论61阅读模式
英文:

Enable EditText underline

问题

默认情况下,EditText 具有下划线。我希望在程序启动时将 EditText 禁用,当有人点击图标时,启用 EditText

下面是一张图片,显示了“to...”的 EditText 没有下划线,而其他的 EditText 都有下划线,而且具有焦点的 EditText 下划线的颜色是强调色。

启用EditText下划线

我尝试过的代码:

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.getBackground().setColorFilter(ContextCompat.getColor(MainActivity.this, R.color.colorAccent), PorterDuff.Mode.SRC_IN);
    }
});

代码末尾的三行代码是我根据这里其他问题的建议尝试过的不同方法。

英文:

By Default EditText has an underline. I want to start my program with the Edittext disabled and when someone clicks on an icon, the EditText is enabled.

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.

启用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.

答案1

得分: 2

如果您想要隐藏和显示 EditText 的下划线,可以按照以下方式进行操作:

隐藏下划线:

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

显示下划线:

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

<hr>

如果您使用的是 androidx,更好的解决方案是:

隐藏下划线:

foodDesiredEditText.setBackgroundResource(0);

显示下划线:

foodDesiredEditText.setBackgroundResource(androidx.appcompat.R.drawable.abc_edit_text_material);
英文:

If you want to hide and show the underline of the EditText, you can do it like the following:

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

<hr>

A better solution if you are using androidx is:

To hide the line:

foodDesiredEditText.setBackgroundResource(0);

To show the line:

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

huangapple
  • 本文由 发表于 2020年10月11日 10:19:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/64300059.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定