简体   繁体   中英

Veryfing a String returns false?

Edit: I am new to Android, I don't want to use databases or etc to store the credentials, I just want to make the if statement to work...

So, I am trying to make a simple login system for an app. After I
press login, the toast returns the false message, but the strings are true, what I am doing wrong when I am trying to verify those? (if statement)

   package com.example.reevo.a02activities07;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;

import static android.R.attr.button;
import static android.R.attr.onClick;
import static android.icu.lang.UCharacter.GraphemeClusterBreak.T;

public class MainActivity extends AppCompatActivity {

    TextView textViewUsername, textViewPassword;
    Button loginButton;

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

        textViewPassword = (TextView) findViewById(R.id.userInput);
        textViewUsername = (TextView) findViewById(R.id.passwordInput);
        loginButton = (Button) findViewById(R.id.buttonLogin);

    }

    public void onClick(View view){

        if (textViewUsername.getText().toString().equals("admin") && textViewPassword.getText().toString().equals("pass")){
            Toast.makeText(getApplicationContext(), "Succesfull!", Toast.LENGTH_SHORT).show();
        }   else Toast.makeText(getApplicationContext(), "Wrong credentials!", Toast.LENGTH_SHORT).show();

    }
}

The textViewPassword is pointing to R.id.userInput and textViewUsername to R.id.passwordInput. You should change it

there is mistake in your binding controll check it use this

textViewPassword = (TextView) findViewById(R.id.passwordInput);
textViewUsername = (TextView) findViewById(R.id.userInput);

insted of this

textViewPassword = (TextView) findViewById(R.id.userInput);
textViewUsername = (TextView) findViewById(R.id.passwordInput);

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