我有一个editText,想将其背景颜色设置为红色,就像这样:
RegistrationCountry.setBackgroundColor(Color.RED);
现在我想要移除这个背景颜色。
使用以下方法存在问题:
RegistrationCountry.setBackgroundColor(Color.TRANSPARENT);
我会失去EditText的轮廓。
我有一个editText,想将其背景颜色设置为红色,就像这样:
RegistrationCountry.setBackgroundColor(Color.RED);
现在我想要移除这个背景颜色。
使用以下方法存在问题:
RegistrationCountry.setBackgroundColor(Color.TRANSPARENT);
我会失去EditText的轮廓。
您可以使用
RegistrationCountry.setBackgroundResource(android.R.drawable.editbox_background);
将背景设置为标准的背景图片。
当您调用任何setBackgroundX()
方法时,问题就会出现,因为它将替换当前背景(即“轮廓”),所以当您调用setBackgroundColor(Color.RED)
时,您会将轮廓替换为红色,然后再用透明度替换红色。您需要做的是用上面的代码行将红色替换为原始背景。
尝试通过以下方式设置背景:
RegistrationCountry.setBackgroundResource(0);
如果您只想突出显示EditText
对象,可以使用PorterDuff:http://developer.android.com/reference/android/graphics/PorterDuff.Mode.html。
要设置颜色:
RegistrationCountry.getBackground().setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN);
去除颜色:
RegistrationCountry.getBackground().clearColorFilter();