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

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex