Android中的简单用户名密码无法正常工作 [英] Simple username password code in android not working
问题描述
在android studio中,我制作了一个简单的用户名和密码代码,如果用户名和密码编辑文本视图中的值均为特定值(例如"cake"和"robot"),则该代码敬酒,但是当我测试并输入时正确的用户名和密码值被告知我输入了错误的匹配项?
In android studio im making a simple username and password code that toasts something if both the values in the username and password edit text views are certain values ("cake" and "robot" for instance) however when I test it and enter the correct username and password values im being told that I have entered the wrong match?
这是主要活动
public class MainActivity extends AppCompatActivity {
EditText usernameET;
EditText passwordET;
Button SignIn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
usernameET = (EditText) findViewById(R.id.username);
passwordET = (EditText) findViewById(R.id.password);
SignIn = (Button) findViewById(R.id.signin);
}
public void SignIn(View view) {
String username = usernameET.getText().toString();
String password = passwordET.getText().toString();
if(username == "cake" && password == "robot"){
Toast.makeText(this, "You Signed In", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "Invalid Login", Toast.LENGTH_SHORT).show();
}
}
}
主要活动布局:
<EditText
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/password"
android:layout_alignStart="@+id/password"
android:layout_marginBottom="15dp"
android:hint="@string/username"
android:inputType="text" />
<EditText
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:hint="@string/password"
android:inputType="text" />
<Button
android:id="@+id/signin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sign_in"
android:onClick="SignIn"
android:layout_below="@+id/password"
android:layout_centerHorizontal="true" />
为什么会这样?
推荐答案
我认为您必须写
username.equals("cake")&& password.equals("robot")
username.equals("cake") && password.equals("robot")
原因是您必须检查edittext而不是引用的内容
Reason is that you have to check the content of the edittext not the reference
当使用==时,它将检查内存中的引用,而必须检查数据.
when you use == it checks reference in the memory whereas you have to check the data.
这篇关于Android中的简单用户名密码无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!