编辑:我是Android新手,我不想使用数据库或等来存储凭据,我只是想让if语句工作......
所以,我正在尝试为应用程序创建一个简单的登录系统。我以后 按登录,toast返回false消息,但是字符串是true,当我试图验证那些时我做错了什么? (如果声明)
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();
}
}
答案 0 :(得分:0)
textViewPassword指向R.id.userInput,textViewUsername指向R.id.passwordInput。你应该改变它
答案 1 :(得分:0)
你的绑定控件有错误检查它 使用这个
textViewPassword = (TextView) findViewById(R.id.passwordInput);
textViewUsername = (TextView) findViewById(R.id.userInput);
这是
的内容textViewPassword = (TextView) findViewById(R.id.userInput);
textViewUsername = (TextView) findViewById(R.id.passwordInput);