2017-10-09 108 views
-1

編輯:我是新來的Android,我不想使用數據庫或等存儲憑據,我只是想使if語句工作...Veryfing一個字符串返回false?

所以,我試圖做一個簡單的登錄系統的應用程序。我在
按登錄,吐司返回虛假消息,但字符串是真實的,當我試圖驗證那些我做錯了什麼? (如果聲明)

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(); 

    } 
} 
+1

嘗試equalIgnoreCase –

+0

如果你的布爾邏輯分解爲單獨的條款,那麼你可以調試哪一個失敗。 – Dragonthoughts

+2

從邏輯上講,如果'textViewUsername.getText()'或'textViewPassword.getText()'沒有返回你的想法,唯一的辦法就是失敗。 – Neil

回答

0

textViewPassword指向R.id.userInput和textViewUsername以R.id.passwordInput。你應該改變它

+0

謝謝,那是原因,我的錯誤。抱歉。 ^^ – ReevoXFR

0

有錯誤在你的綁定控制研究檢查 使用

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

insted的這個

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