简体   繁体   中英

get String from EditText

login.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            if( username_text.getText().toString() == "admin" &&
                    pass_text.getText().toString() == "password"){

                startActivityForResult(i, ACTIVITY_CREATE);

            }else{
                Toast.makeText(gps_gui.this, "Your username or password is not correct! Please, insert again",
                        Toast.LENGTH_SHORT).show();
                clearEditText();
            }
        }

    });

I'm try to check it, I found it not go to if condition.

When using java, you must compare Strings using the String.equals(String) method. The == comparison checks to see if the String object values are equal, which undoubtedly they are not. Try to change you example to :

"admin".equals(username_text.getText().toString()) &&"password".equals(pass_text.getText().toString())

Its also smart to put the static string first, in case the string value of the value being checked is null.

Use String class's equals() method to compare Strings. The following links would give you more details.

About equals(Object yourObj) method (CASE sensitive)

equalsIgnoreCase(String yourStringObject) can be used, if you want it to be not case sensitive!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM