package arabiannight.tistroy.com;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class TestTextWatcherActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setLayout();
etTexts[EMAIL].addTextChangedListener(emailWatcher);
etTexts[NAME].addTextChangedListener(nameWatcher);
etTexts[PHONE].addTextChangedListener(phoneWatcher);
for(Button b : btButtons){
b.setEnabled(false);
}
}
public void onButtonClick(View v){
switch (v.getId()) {
case R.id.et_box_01:
break;
case R.id.et_box_02:
break;
case R.id.et_box_03:
break;
case R.id.bt_confirm:
if(btButtons[CONFIRM].isEnabled()){
Toast.makeText(getApplicationContext(),
"EMAIL = " + etTexts[EMAIL].getText().toString() +
" 등록 되었습니다. , "+
"NAME = " + etTexts[NAME].getText().toString() +
" 등록 되었습니다. , "+
"PHONE = " + etTexts[PHONE].getText().toString() +
" 등록 되었습니다.",
Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(), "모두 입력해 주십시오.",
Toast.LENGTH_SHORT).show();
}
break;
}
}
private TextWatcher emailWatcher = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(s.length() > 0){
btButtons[EMAIL].setEnabled(true);
}else{
btButtons[EMAIL].setEnabled(false);
}
setEnable(setButtonEnable(etTexts[EMAIL], etTexts[NAME], etTexts[PHONE]));
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
};
private TextWatcher nameWatcher = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(s.length() > 0){
btButtons[NAME].setEnabled(true);
}else{
btButtons[NAME].setEnabled(false);
}
setEnable(setButtonEnable(etTexts[EMAIL], etTexts[NAME], etTexts[PHONE]));
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
};
private TextWatcher phoneWatcher = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(s.length() > 0){
btButtons[PHONE].setEnabled(true);
}else{
btButtons[PHONE].setEnabled(false);
}
setEnable(setButtonEnable(etTexts[EMAIL], etTexts[NAME], etTexts[PHONE]));
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
};
private void setEnable(boolean isEnable){
btButtons[CONFIRM].setEnabled(isEnable);
}
private boolean setButtonEnable(EditText email, EditText name, EditText phone ) {
boolean isAllFull = false;
if (email != null && name.length() > 0
&& name != null && name.length() > 0
&& phone != null && phone.length() > 0) {
isAllFull = true;
}
return isAllFull;
}
private static final int EMAIL = 0;
private static final int NAME = 1;
private static final int PHONE = 2;
private static final int CONFIRM = 3;
private EditText[] etTexts;
private Button[] btButtons;
public void setLayout(){
etTexts = new EditText[]{
(EditText)findViewById(R.id.et_box_01),
(EditText)findViewById(R.id.et_box_02),
(EditText)findViewById(R.id.et_box_03)
};
btButtons = new Button[]{
(Button)findViewById(R.id.bt_check_01),
(Button)findViewById(R.id.bt_check_02),
(Button)findViewById(R.id.bt_check_03),
(Button) findViewById(R.id.bt_confirm)
};
}
}
TestTextWatcher.zip