我的 Activity 有三个 editText
和 一个 button
. 如果三个editText
都有字,button
就可以按,不然就按不到。问题是我的button
一直按不到就算三个editText
都有字。
Button buttonSave;
public TextWatcher mTextWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
@Override
public void afterTextChanged(Editable editable) {
// check Fields For Empty Values
checkFieldsForEmptyValues();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonSave = (Button) findViewById(R.id.buttonSave);
buttonSave.setBackgroundColor(Color.parseColor("#D3D3D3"));
buttonSave.setEnabled(false);
editTextName = (EditText) findViewById(R.id.editTextName);
editTextPassword = (EditText) findViewById(R.id.editTextPassword);
editTextConfirm = (EditText) findViewById(R.id.editTextConfirmPassword);
//Click Listener for button
buttonSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String pass = editTextPassword.getText().toString();
String confirm = editTextConfirm.getText().toString();
String name = editTextName.getText().toString();
if ((editTextName.getText().toString().trim().length() == 0) || (editTextPassword.getText().toString().trim().length() == 0) || (editTextConfirm.getText().toString().trim().length() == 0)) {
Toast.makeText(getApplicationContext(), "Field cannot be null", Toast.LENGTH_LONG).show();
}
else if (!(pass.equals(confirm))) {
Toast.makeText(getApplicationContext(), "password and confirm password not same ", Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), pass + confirm, Toast.LENGTH_SHORT).show();
}
else
{
Intent intent = new Intent(MainActivity.this, AddMonthlyExpenses.class);
intent.putExtra("name", name);
startActivity(intent);
}
});
}
public void checkFieldsForEmptyValues(){
if((editTextName.getText().toString().trim().length() == 0) || (editTextPassword.getText().toString().trim().length() == 0) || (editTextConfirm.getText().toString().trim().length() == 0)){
buttonSave.setEnabled(false);
buttonSave.setBackgroundColor(Color.parseColor("#D3D3D3"));
} else {
buttonSave.setEnabled(true);
buttonSave.setBackgroundColor(Color.parseColor("#FFAA66CC"));
}
}
还有 mTextWatcher never used. 求大侠帮帮忙!
buttonSave.setEnabled(false);
mTextWatcher를 한 번도 사용한 적이 없으면 버튼 저장이 항상 비활성화되어 클릭할 수 없게 됩니다.