Reputation: 15034
I have a username and password field and now i need to check and redirect him to the next page in Android.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText loginText = (EditText) findViewById(R.id.widget44);
final EditText loginPassword = (EditText) findViewById(R.id.widget47);
final Button button = (Button) findViewById(R.id.widget48);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = null;
if(loginText.getText().equals("admin") &&
loginPassword.getText().equals("admin")) {
System.out.println("Entering");
myIntent = new Intent(view.getContext(), Page1.class);
} else {
}
startActivity(myIntent);
}
});
}
Temp now i am checking by hardcoding the values, but this too does not work for me. Why? I usually check in Java like this, why does it not accept me the same way in Android
Upvotes: 6
Views: 13111
Reputation: 9629
I am finding only a small problem. In your code, correct it as shown below, and I think it will work:
if(loginText.getText().**toString()**.equals("admin") &&
loginPassword.getText()**.toString()**.equals("admin")) {
System.out.println("Entering");
myIntent = new Intent(view.getContext(), Page1.class);
} else {
...
}
See the correction in asterisks.
Upvotes: 1
Reputation: 1537
Don't you need to put toString()
to your textelements?
Like this:
if(loginText.getText().toString().equals("admin") &&
loginPassword.getText().toString().equals("admin")) {
...
Edit: neutrino was faster (+1) :)
Upvotes: 2
Reputation: 24164
I think it's because EditText#getText()
returns an Editable
object. Try
if(loginText.getText().toString().equals("admin") &&
loginPassword.getText().toString().equals("admin")) {
...
}
Upvotes: 5