英文:
Enable EditText underline
问题
默认情况下,EditText
具有下划线。我希望在程序启动时将 EditText
禁用,当有人点击图标时,启用 EditText
。
下面是一张图片,显示了“to...”的 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.
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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论