我有一个使用ImageView和drawable默认为白色的Icon。我希望按下时颜色变为黑色。当EditText行获得文本时(因为用户在行上输入了一些数据),我希望图标变为灰色。目前,当长度测试> 0时,图标变为黑色,并且当按下图标时没有任何变化(因为颜色已经是黑色)。当长度测试然后进行== 0时,颜色应该恢复为默认颜色白色时保持黑色。我在这里缺少什么?
活动文件:
protected void OnCreate...
fEditText = (EditText) findViewById(R.id.FEditText);
@Override
public void afterTextChanged(Editable s) {
fTextInputLayout.setHint("Due Date");
if (fEditText.getText().length() > 0) {
imgcan = (ImageView) findViewById(R.id.imageView1);
imgcan.setImageResource(R.drawable.ic_delete_54per24);
<!-- "ic_delete_54per24 is the the gray icon drawable -->
}
}
Xml布局文件:
...
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:onClick="onClickClearDate"
android:clickable="true"
android:src="@drawable/trash_can" />
trash_can drawable file:
...
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/ic_delete_black_24dp" />
<!-- black color when pressed -->
<item android:drawable="@drawable/ic_delete_white_24dp" />
<!-- white color when normal (default) -->
答案 0 :(得分:1)
也许你错过了在文字为空时重置彩色图像,如下所示:
if (fEditText.getText().length() > 0) {
imgcan = (ImageView) findViewById(R.id.imageView1);
imgcan.setImageResource(R.drawable.ic_delete_54per24);
}else{ //add this
imgcan = (ImageView) findViewById(R.id.imageView1);
imgcan.setImageResource(R.drawabale.ic_delete_white_24dp);
}
答案 1 :(得分:1)
尝试使用Textwatcher?
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(TextUtils.isEmpty(s)){
imageView.setImageResource(R.drawable.ic_delete_54per24);
}
}
@Override
public void afterTextChanged(Editable s) {
}
});