当两个Button
不为空时,如何更改EditText
的笔触颜色?
代码:
if (editText1.getText().toString().trim().length() > 0 && editText2.getText().toString().trim().length() > 0) {
btn.setBackgroundResources(R.drawable.change_stroke);
} else {
btn.setBackgroundResources(R.drawable.change_normal);
}
答案 0 :(得分:0)
使用文本观察器。 onTextChanged
在那里做你的魔法。
private class MyTextWatcher implements TextWatcher {
private View view;
private MyTextWatcher(View view) {
this.view = view;
}
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//set the default button stroke color
}
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//set the final button stroke color
}
}
}
使用可绘制文件或资源文件
定义颜色答案 1 :(得分:0)
您应为每个TextWatcher
实施并注册EditText
。
使用一些代码:
// lookup for Views
final EditText e1 = (EditText) rootView.findViewById(R.id.editText);
final EditText e2 = (EditText) rootView.findViewById(R.id.editText2);
final Button btn = (Button) rootView.findViewById(R.id.button);
// TextWatcher definitions (to react on text changes)
TextWatcher tw = 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 (e1.getText().length() > 0 && e2.getText().length() > 0) {
btn.setBackgroundResources(R.drawable.change_stroke);
} else {
btn.setBackgroundResources(R.drawable.change_normal);
}
}
@Override
public void afterTextChanged(Editable s) { }
}
// text watcher registration to the 2 EditText
e1.addTextChangedListener(tw);
e2.addTextChangedListener(tw);