Skip to main content

How to disable/enable button when the edit text is empty/nonempty



i have 2 edit text and 1 button, how can i make this button disable until the user fill all the edit text and i used this code but when i run this code it always disable the button even when i fill the 2 Edit text,also i do not know what the onTextChanged and beforeTextChanged doing !! please help me,i will appreciate it.







public class TestActivity extends Activity {

/** Called when the activity is first created. */

EditText edit1;

EditText edit2;

EditText edit3;

Button button;

String test1;





@Override

protected void onCreate(Bundle savedInstanceState) {

// Your initialization code...

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.main);



edit1= (EditText) findViewById(R.id.edit1);

edit2= (EditText) findViewById(R.id.edit2);



button=(Button) findViewById(R.id.button);









TextWatcher watcher = new LocalTextWatcher();

edit1.addTextChangedListener(watcher);

edit2.addTextChangedListener(watcher);



updateButtonState();

}



void updateButtonState() {

boolean enabled;

if(enabled = checkEditText(edit1)

&& checkEditText(edit2)){

button.setEnabled(enabled);}

}

}



private boolean checkEditText(EditText edit) {

return Integer.getInteger(edit1.getText().toString()) != null;

}



private class LocalTextWatcher implements TextWatcher {

public void afterTextChanged(Editable s) {

updateButtonState();

}



public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}



public void onTextChanged(CharSequence s, int start, int before, int count) {

}

}









}}}




Comments

  1. Monerah change your checkEditText method for:

    private boolean checkEditText(EditText edit) {
    return edit.getText().length() == 0;
    }


    and your updateButtonState() for:

    void updateButtonState() {
    if(checkEditText(edit1) && checkEditText(edit2)) button.setEnabled(false);
    else button.setEnabled(true);
    }


    That would make it work right.

    As an additional advice, I would change checkEditText's name for isEditTextEmpty or something more representative to what it does. It would make the if statement much more readable :)

    Regarding your question on what the onTextChanged and beforeTextChanged methods do, take a look at the following:

    beforeTextChanged(CharSequence s, int start, int count, int after).
    This means that the characters are about to be replaced with some new text. The text is uneditable.
    Use: when you need to take a look at the old text which is about to change.

    onTextChanged(CharSequence s, int start, int before, int count).
    Changes have been made, some characters have just been replaced. The text is uneditable.
    Use: when you need to see which characters in the text are new.

    afterTextChanged(Editable s).
    The same as above, except now the text is editable.
    Use: when you need to see and possibly edit new text.

    The first two methods are of no use to what you are trying to do so with afterTextChanged you are done.
    Hope it helps.

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.