0

I am trying to work with editText for my android app log in screen. No matter how I try to get it to work it never does! Currently I'm just trying to compare the editText field to a single letter string, then if it matches call a function to start a new activity.

Any help is greatly appreciated! My end goal is to make two toast messages. If there is not input, then it says "Please enter username and password" and if it is wrong it will say "Wrong username password combination". Then if it matches, the app will simply go to the next page, titled "Device Page". Thanks again!!

package com.example.chirp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class LogIn extends Activity{

EditText edit_text; 
//  
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.loginscreen);

        edit_text   = (EditText)findViewById(R.id.editText2);
        login       = (Button)findViewById(R.id.button1);
        login.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View view)
                {
//                  Log.v("EditText value=", edit_text.getText().toString());   
                if(edit_text.getText().toString() == "d")
        {
            newscreen();
            }
            }
         });
}
    public void newscreen(){
        Intent i = new Intent(this, Devices.class);
        startActivity (i);
    }   
}
  • 2
    You need to use equals instead of == ;) http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – Gabriel Apr 28 '14 at 16:18
  • What are `login` and `Edit` variables? Can't see them... – Onik Apr 28 '14 at 16:21
  • Thank you! I accidentally deleted the line 'login = (Button)findViewById(R.id.button1)' And I changed Edit to edit_text. Sorry for the confusion but it is updated now! – user3416852 Apr 28 '14 at 16:24
  • This is a perfectly okay question according to me, I don't think it required a downvote whosoever did it. – Shishir Gupta Apr 28 '14 at 16:32

2 Answers2

3
Edit.getText().toString() == "d"

Strings should be compared with the .equals() method instead. So change it to be:

Edit.getText().toString().equals("d");

If you mean it even to be case insensitive, you can use:

Edit.getText().toString().equalsIgnoreCase("d");
nKn
  • 13,691
  • 9
  • 45
  • 62
0

NOTE: to compare strings, use equals().

Assuming that you want to only compare the string on click of any button to go to different screen :

if(edit_text.getText().toString().equals("Your String"))
    {
        yourSomeScreen();
    }

If you want to compare the string while typing in the edit text field, you need to use TextWatcher like this:

http://www.learn-android-easily.com/2013/06/using-textwatcher-in-android.html

Hope this helps..:)

mike20132013
  • 5,357
  • 3
  • 31
  • 41