簡體   English   中英

android無法獲取文本並進行登錄

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

我正在嘗試創建我的Android應用程序的登錄頁面。 我有2個editview和一個按鈕。 我正在使用靜態用戶憑據登錄。 當我運行該應用程序並嘗試使用正確的用戶名和密碼登錄時,它會提供不正確的憑據消息。

這是我的代碼,這是一段非常簡單的代碼

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!");
    }
}

您應該使用equalsequalsIgnoreCase而不是==來比較字符串

例:

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

您正在使用==比較字符串。

使用.equals插入。

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

檢查密碼時,請使用gettext().toString().equals("goodpass")代替== ==比較參考,而不是值。

使用.equals

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

==運算符確定2個引用是否指向同一對象。

==不用於比較字符串的內容。 試試user_name.getText().toString().equals("rotanet")

==測試引用相等性。

使用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