2017-10-09 45 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);