这篇文章是网上的资源。
public class SomeMonitorEditText implements TextWatcher {
private Button button;
private EditText[] text;
public void SetMonitorEditText(final Button button, final EditText... text) {
this.button = button;
this.text = text;
for (int i = 0; i < text.length; i++) {
if (text[i] != null) {
text[i].addTextChangedListener(SomeMonitorEditText.this);
}
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
for (int i = 0; i < text.length; i++) {
if (text[i].length() == 0) {
button.setBackgroundResource(R.drawable.graylong_btn);
button.setEnabled(false);
return;//这句代码值两千万
} else {
button.setBackgroundResource(R.drawable.red_btn);
button.setEnabled(true);
}
}
}
}
主函数的布局文件就不写了,见图片
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText et1 = (EditText) findViewById(R.id.et1);
EditText et2 = (EditText) findViewById(R.id.et2);
EditText et3 = (EditText) findViewById(R.id.et3);
EditText et4 = (EditText) findViewById(R.id.et4);
Button btn = (Button) findViewById(R.id.btn);
new SomeMonitorEditText().SetMonitorEditText(btn, et1, et2, et3, et4);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "被点击了", Toast.LENGTH_SHORT).show();
}
});
}
}