Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing text in EditText

Tags:

android

My code:

final EditText et1 = (EditText)findViewById(R.id.et1);

        
        et1.setOnFocusChangeListener(new OnFocusChangeListener(){
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if ( hasFocus )
                {
                    et1.setTextColor(0xFF000000);
                    if ( et1.getText().equals("Email") ) { et1.setText(""); }
                }
                else
                { 
                    et1.setTextColor(0xFF7F7F7F);
                    if ( et1.getText().equals("") ) { et1.setText("Email"); }
                }
            }           
        });

et1 has "Email" in it by default. When the user clicks on it, I want it to clear. The OnFocusChangeListener works fine as the color changes, but the text does not change, i.e. the

if ( et1.getText().equals("Email") )

doesn't fire. Nor does the equals(""), after I clear the et.

What am I doing wrong?

like image 699
Roger Travis Avatar asked Aug 08 '26 11:08

Roger Travis


1 Answers

Try comparing et1.getText().toString().equals("Email") instead.

like image 54
zmbq Avatar answered Aug 10 '26 23:08

zmbq



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!