本文介绍了从EditText上与数据库中的字符串比较文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这code应该比较文本输入的 R.id.editUserName 是 R.string.DB_username ,如果他们匹配,在您登录,否则秀举杯,他们不匹配。
This code should compare text entered into R.id.editUserName with R.string.DB_username and, if they match, log you in, else show a toast that they don't match.
public void signIn(View view) {
EditText editUserName = (EditText)findViewById(R.id.editUserName);
String userName = editUserName.getText().toString();
if ( userName == getResources().getString(R.string.DB_username)) {
// log in
setContentView(R.layout.screen1);
} else {
// show toast
Toast toast = Toast.makeText(getApplicationContext(), userName+" != "+getResources().getString(R.string.DB_username), Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
}
即使当他们这样做匹配,它仍然显示敬酒,如罗杰!罗杰=...怎么会这样呢?
Even when they do match, it still shows a toast, such as "Roger != Roger"... how could that be?
推荐答案
不要对它们进行比较像。使用Java提供的字符串比较函数来比较它们。
Dont compare them like that. Use the string comparison function provided by java to compare them.
这里指:
这篇关于从EditText上与数据库中的字符串比较文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!