简体   繁体   English

android无法获取文本并进行登录

[英]android cannot get the text and do the login

I am trying to create a login page to my Android App. 我正在尝试创建我的Android应用程序的登录页面。 I have 2 editview and a button. 我有2个editview和一个按钮。 I am using a static user credentials to login. 我正在使用静态用户凭据登录。 when I run the app, and try to login with the correct username and password, it gives incorrect credential message. 当我运行该应用程序并尝试使用正确的用户名和密码登录时,它会提供不正确的凭据消息。

here is my code, it is a very simple piece of code 这是我的代码,这是一段非常简单的代码

public void sendMessage(View view) {

    EditText user_name = (EditText)findViewById(R.id.txt_username);
    EditText password =(EditText)findViewById(R.id.txt_password);

    if(user_name.getText().toString()=="rotanet" && password.getText().toString()=="rotanet"){
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        TextView lbl_error = (TextView)findViewById(R.id.lbl_error);
        lbl_error.setText("");
    }
    else{
        TextView lbl_error = (TextView)findViewById(R.id.lbl_error);
        lbl_error.setText("wrong credentials!");
    }
}

You should use equals or equalsIgnoreCase instead of == to compare strings 您应该使用equalsequalsIgnoreCase而不是==来比较字符串

Example: 例:

 if(user_name.getText().toString().equals("rotanet") && password.getText().toString().equals("rotanet"))
{
stuff
}

You are using == to compare strings. 您正在使用==比较字符串。

Use .equals insted. 使用.equals插入。

if(user_name.getText().toString().equals("rotanet") && password.getText().toString().equals("rotanet")){

Use gettext().toString().equals("goodpass") instead of == when you check the password. 检查密码时,请使用gettext().toString().equals("goodpass")代替== == compares reference, not value. ==比较参考,而不是值。

Use .equals 使用.equals

if(user_name.getText().toString().equals("rotanet") && password.getText().toString().equals("rotanet"))
{
 //dosomething  
}

the == operator determines if 2 references point to the same object. ==运算符确定2个引用是否指向同一对象。

== is not used to compare contents of strings. ==不用于比较字符串的内容。 Try user_name.getText().toString().equals("rotanet") 试试user_name.getText().toString().equals("rotanet")

== tests for reference equality. ==测试引用相等性。

use equals method for comparison 使用equals方法进行比较

if((user_name.getText().toString().equals("rotanet") && password.getText().toString().equals("rotanet"))
{

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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