[英]Text doesn't change in variable after pressing button in Android
For a little android project I'm making a loginscreen.对于一个小的 android 项目,我正在制作一个登录屏幕。 I have variables, but they stay empty once I fill them in and press the button.
我有变量,但是一旦我填写它们并按下按钮,它们就会保持为空。
these are my variables:这些是我的变量:
private EditText email;
private EditText wachtwoord;
private Button loginButton;
here I fill them:我在这里填写它们:
email = (EditText)findViewById(R.id.emailInput);
wachtwoord = (EditText)findViewById(R.id.wachtwoordInput);
loginButton = (Button)findViewById(R.id.loginButton);
}
}
and my method which always executes the catch code和我的方法总是执行捕获代码
public void login(View view)
{
try {
if(email.getText().toString() == "jolt_koens@hotmail.com" && wachtwoord.getText().toString() == "admin")
{
Toast.makeText(getApplicationContext(), "gelukt", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getApplicationContext(), "mislukt", Toast.LENGTH_SHORT).show();
loginButton.setEnabled(false);
}
Compare strings using either of these methods使用这些方法之一比较字符串
method 1:方法一:
if(((email.getText().toString().compareTo("jolt_koens@hotmail.com")==0) && (wachtwoord.getText().toString().compareTo("admin")==0))
{
do something
}
method 2:方法二:
if(((email.getText().toString().equals("jolt_koens@hotmail.com") )&& (wachtwoord.getText().toString().equals("admin")))
{
do something
}
public void login(View view)
{
try {
if(email.getText().toString() .equals ("jolt_koens@hotmail.com") && wachtwoord.getText().toString().equals( "admin"))
{
Toast.makeText(getApplicationContext(), "gelukt", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getApplicationContext(), "mislukt", Toast.LENGTH_SHORT).show();
loginButton.setEnabled(false);
}
Try the above code.试试上面的代码。 Use .equals() instead of == To understand the difference between the two read this .
使用 .equals() 而不是 == 要了解两者之间的区别,请阅读此。
Change this line:改变这一行:
if(email.getText().toString() == "jolt_koens@hotmail.com" && wachtwoord.getText().toString() == "admin")
to this:对此:
if(email.getText().toString().equals("jolt_koens@hotmail.com") && wachtwoord.getText().toString().equals("admin"))
if(email.getText().toString().equalsIgnoreCase("jolt_koens@hotmail.com") && wachtwoord.getText().toString().equalsIgnoreCase ("admin"))
{
Toast.makeText(getApplicationContext(), "gelukt", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getApplicationContext(), "mislukt", Toast.LENGTH_SHORT).show();
loginButton.setEnabled(false);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.