How to handle EditText that cannot be null string


There are two ways to handle editText that cannot be null string.

a. Check null string before INSERT to DB.
This way user may confuse because user cannot see the exact value.
However, this solution is easy to implement.

b. Check instantly when user type any text.
This way let user see the exact value by instantly change null string in the editText to " " or "0" or what ever you want.

The sample source code.

public void onCreate(Bundle bundleSavedInstanceState) {

super.onCreate(bundleSavedInstanceState);
editTextBtnStringValue[type][subType].setOnFocusChangeListener(this);

}

This method aim to prevent null value by update the Global variable which is currentFocusedEditText then call setNullCurretFocusedEditText()

@Override
public void onFocusChange(View view, boolean b) {
currentFocusedEditText = (EditText)view;
setNullCurretFocusedEditText();

}

public void setNullCurretFocusedEditText(){

if(currentFocusedEditText==null){
L.error("currentFocusedEditText=null");
return;
}
if(currentFocusedEditText.getText().toString().trim().length() <= 0){
currentFocusedEditText.setText("0");
}

}
These three methods need to implement for TextWatcher but this project use only onTextChanged.

@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {

}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
setNullCurretFocusedEditText();
}

@Override
public void afterTextChanged(Editable editable) {


}

Feel free to leave your questions or suggestions ^^.

Ref.
http://stackoverflow.com/questions/11134144/android-edittext-onchange-listener/17583291#17583291
http://stackoverflow.com/questions/4310525/android-on-edittext-changed-listener
http://stackoverflow.com/questions/12191394/change-edittext-text-from-ontextchangelistener
http://looksok.wordpress.com/2012/09/08/action-on-edittext-change-listener/

Popular posts from this blog

เทคนิค ชนะ เกมเศรษฐี 14 ตารวด!!! ( Let's get Rich )

Using Google Chrome Inspect Element to Analyze the website

Easy way to download whole youtube play list and convert them to mp3