如果没有输入任何文本并且没有点击它,那么如何使EditText(下划线和android:提示)具有50%的透明度,然后在点击或更改时更改为100%输入文字?
此外,当透明度为100%(单击或输入文本时)时,如何将下划线(不提示)的颜色更改为橙色?
答案 0 :(得分:2)
使用以下代码
mEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
// If has focus then change the alpha to 1
mEditText.setAlpha(1);
// Set orange color filter
mEditText.getBackground().setColorFilter(Color.argb(255, 255, 165, 0), PorterDuff
.Mode.SRC_IN);
} else {
// When focus is lost, check if some text has been entered.
// If not then set the alpha to 0.5f (50% transparent)
if (TextUtils.isEmpty(mEditText.getText().toString())) {
mEditText.setAlpha(0.5f);
// Clear the color filter
mEditText.getBackground().clearColorFilter();
}
}
mEditText.invalidate();
}
});