Android 获取密码编辑文本值时出错
Android error when getting password edit text value
我得到了 password-edit-text 值,但我没有得到我想要的。
我获取值的代码:
String username = "", password = "";
username = ((EditText)findViewById(R.id.username_pet)).toString();
password = ((EditText)findViewById(R.id.password_pet)).toString();
但是当我将结果放入 Toast 时,我做错了。我得到的是 this
错误。
任何帮助将不胜感激。谢谢!
您的代码会 return 您 EditText
的 ID,但您想要的是文本。
试试这个
将其替换为:
username = ((EditText)findViewById(R.id.username_pet)).toString();
password = ((EditText)findViewById(R.id.password_pet)).toString();
这个
username = ((EditText)findViewById(R.id.username_pet)).getText();
password = ((EditText)findViewById(R.id.password_pet)).getText();
希望对您有所帮助
username = ((EditText)findViewById(R.id.username_pet)).getText().toString();
password = ((EditText)findViewById(R.id.password_pet)).getText().toString();
发生这种情况是因为您试图在获取输入字段的文本之前转换为字符串。该应用程序完全按照您的要求执行:获取 EditText 视图并将视图转换为字符串,而不是视图中的文本。如果说得通。
尝试
EditText userName = (EditText) findViewById(R.id.username_pet);
String userNameFromEditText = (String) username.getText().toString();
然后toast userNameFromEditText的值。
我得到了 password-edit-text 值,但我没有得到我想要的。
我获取值的代码:
String username = "", password = "";
username = ((EditText)findViewById(R.id.username_pet)).toString();
password = ((EditText)findViewById(R.id.password_pet)).toString();
但是当我将结果放入 Toast 时,我做错了。我得到的是 this 错误。
任何帮助将不胜感激。谢谢!
您的代码会 return 您 EditText
的 ID,但您想要的是文本。
试试这个
将其替换为:
username = ((EditText)findViewById(R.id.username_pet)).toString();
password = ((EditText)findViewById(R.id.password_pet)).toString();
这个
username = ((EditText)findViewById(R.id.username_pet)).getText();
password = ((EditText)findViewById(R.id.password_pet)).getText();
希望对您有所帮助
username = ((EditText)findViewById(R.id.username_pet)).getText().toString();
password = ((EditText)findViewById(R.id.password_pet)).getText().toString();
发生这种情况是因为您试图在获取输入字段的文本之前转换为字符串。该应用程序完全按照您的要求执行:获取 EditText 视图并将视图转换为字符串,而不是视图中的文本。如果说得通。
尝试
EditText userName = (EditText) findViewById(R.id.username_pet);
String userNameFromEditText = (String) username.getText().toString();
然后toast userNameFromEditText的值。