繁体   English   中英

非常确定一个字符串会返回假吗?

[英]Veryfing a String returns false?

编辑:我是 Android 新手,我不想使用数据库等来存储凭据,我只想让 if 语句起作用...

所以,我正在尝试为应用程序制作一个简单的登录系统。 在我之后
按登录,吐司返回错误消息,但字符串是真的,当我尝试验证这些字符串时,我做错了什么? (如果声明)

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

    }
}

textViewPassword 指向 R.id.userInput,textViewUsername 指向 R.id.passwordInput。 你应该改变它

你的绑定控制有错误检查它使用这个

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

受此影响

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

暂无
暂无

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

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