Decision coming straight to Else

Asked

Viewed 34 times

-1

I want to call another screen from the Intent, but the decision is going straight to Else. Can anyone help?

package com.example.login;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.app.AlertDialog;
import android.widget.Button;
import android.widget.EditText;

import android.app.Activity;

public class MainActivity extends Activity {

private static final String USUARIO = "usuario";
private static final String SENHA = "123456";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button btlogar = (Button) findViewById(R.id.login);
    @SuppressLint("WrongViewCast") final EditText txtusuario = (EditText) findViewById(R.id.usuario);
    @SuppressLint("WrongViewCast") final EditText txtsenha = (EditText) findViewById(R.id.senha);

    btlogar.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View view) {
            if(txtusuario.equals(USUARIO) || (txtsenha.equals(SENHA))){
                Intent logado = new Intent(MainActivity.this, Logado.class);
                startActivity(logado);
            }else{
                    (new AlertDialog.Builder(MainActivity.this)).setTitle("Aviso")
                            .setMessage("Senha ou usuário inválidos.")
                            .setNeutralButton("OK", null)
                            .show();
                    return;
                }
            }
    });
}

}

//Logado.java

package com.example.login;

import android.os.Bundle;
import android.os.Bundle;
import android.app.Activity;


public class Logado extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.logado);
}}

1 answer

1

You must make the condition using the text typed in Edittext like this:

if(txtusuario.getText().equals(USUARIO) || (txtsenha.getText().equals(SENHA)))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.