Android 中的 String 和 EditText 函数问题
我试图通过检查 EditText 字段中的用户输入来触发条件。当我将字符串从 EditText 打印到 logcat 时,我可以看到数据更改,但检查值的字符串函数始终返回 false。
if(((EditText)findViewById(R.id.drv_in)).getText().toString().equals("")) {
TX_FAIL_TEXT = "Missing Driver ID!";
}
Log.e("SMSDRVERR", ((EditText)findViewById(R.id.drv_in)).getText().toString());
此代码始终显示“缺少驱动程序 ID!”。我已经尝试过这些其他条件,但没有成功:
(((EditText)findViewById(R.id.drv_in)).getText().toString().isEmpty()) //does not compile, says cannot find symbol, but the function is in the Android documentation
(((EditText)findViewById(R.id.drv_in)).getText().toString().length() < 1) //returns false, even for strings of length > 1
我可以通过查看 logcat 并看到我的数据显示在日志中来确认数据确实不为空。条件有什么问题?
如果您在第一次传输中不插入任何数据,它不会失败。如果第一次传输失败,则无论您是否更改数据,所有后续传输都会失败。此外,如果它通过了第一次传输,它将通过所有后续传输。
此外,还有其他条件,发布在下面的完整代码中,它们也仅在第一次单击按钮时进行评估。
transmit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//data validation
///////////////////////
boolean valid = true;
if(((EditText)findViewById(R.id.drv_in)).getText().toString().equals("")) {
TX_FAIL_TEXT = "Missing Driver ID!";
showDialog(DIALOG_FAIL);
TX_FAIL_TEXT = "Transmission Failed!"; //reset the dialog fail text to default
valid = false;
}
Log.e("SMSDRVERR", ((EditText)findViewById(R.id.drv_in)).getText().toString());
if(custSpn.getSelectedItemPosition() == 0) {
TX_FAIL_TEXT = "Missing Customer Selection!";
showDialog(DIALOG_FAIL);
TX_FAIL_TEXT = "Transmission Failed!"; //reset the dialog fail text to default
valid = false;
}
if(prdSpn.getSelectedItemPosition() == 0) {
TX_FAIL_TEXT = "Missing Product Selection!";
showDialog(DIALOG_FAIL);
TX_FAIL_TEXT = "Transmission Failed!"; //reset the dialog fail text to default
valid = false;
}
if(((Cursor)prdSpn.getItemAtPosition(prdSpn.getSelectedItemPosition())).getString(prdSpn.getSelectedItemPosition()).contains("CAR") ||
((Cursor)prdSpn.getItemAtPosition(prdSpn.getSelectedItemPosition())).getString(prdSpn.getSelectedItemPosition()).contains("AUTO") ||
((Cursor)prdSpn.getItemAtPosition(prdSpn.getSelectedItemPosition())).getString(prdSpn.getSelectedItemPosition()).contains("TRUCK")
) {
//must have make, license# and 1vin
if(((EditText)findViewById(R.id.make_in)).getText().toString().equals("")) {
TX_FAIL_TEXT = "Vehicle Entry:\n Missing Make/Model!";
showDialog(DIALOG_FAIL);
TX_FAIL_TEXT = "Transmission Failed!"; //reset the dialog fail text to default
valid = false;
}
if(((EditText)findViewById(R.id.tag_in)).getText().toString().equals("")) {
TX_FAIL_TEXT = "Vehicle Entry:\n Missing Tag Number!";
showDialog(DIALOG_FAIL);
TX_FAIL_TEXT = "Transmission Failed!"; //reset the dialog fail text to default
valid = false;
}
if(((EditText)findViewById(R.id.vin1_in)).getText().toString().equals("") ||
((EditText)findViewById(R.id.vin2_in)).getText().toString().equals("") ||
((EditText)findViewById(R.id.vin3_in)).getText().toString().equals("") ||
((EditText)findViewById(R.id.vin4_in)).getText().toString().equals("") ||
((EditText)findViewById(R.id.vin5_in)).getText().toString().equals("") ||
((EditText)findViewById(R.id.vin6_in)).getText().toString().equals("") ||
((EditText)findViewById(R.id.vin7_in)).getText().toString().equals("") ||
((EditText)findViewById(R.id.vin8_in)).getText().toString().equals("")
) {
TX_FAIL_TEXT = "Vehicle Entry:\n Missing VIN Number!";
showDialog(DIALOG_FAIL);
TX_FAIL_TEXT = "Transmission Failed!"; //reset the dialog fail text to default
valid = false;
}
}
//Log.e("smsDRVERR",((EditText)smsActivity.this.findViewById(R.id.drv_in)).getText().toString());
//begin transmission
///////////////////////
if(valid) {
showDialog(DIALOG_TX_PROGRESS);
Thread t = new Thread(txRunnable);
t.start();
} else {
//do things if needed
}
}
i'm trying to trigger a conditional by checking the user input in an EditText field. when i print the String from the EditText to logcat, i can see the data change, but the String functions that check against the values always return false.
if(((EditText)findViewById(R.id.drv_in)).getText().toString().equals("")) {
TX_FAIL_TEXT = "Missing Driver ID!";
}
Log.e("SMSDRVERR", ((EditText)findViewById(R.id.drv_in)).getText().toString());
this code always displays "Missing Driver ID!". i have tried these other conditionals, with no success:
(((EditText)findViewById(R.id.drv_in)).getText().toString().isEmpty()) //does not compile, says cannot find symbol, but the function is in the Android documentation
(((EditText)findViewById(R.id.drv_in)).getText().toString().length() < 1) //returns false, even for strings of length > 1
i can confirm that the data is, indeed, no null by looking at logcat and seeing my data show up in the logs. what's wrong with the conditional?
it doesn't fail if you insert no data in the first transmit. if the first transmit fails, all subsequent transmissions fail, regardless of whether you change the data or not. furthermore, if it passes the first transmission, it will pass all subsequent transmissions.
additionally, there are other conditionals, posted in the full code below, which also evaluate only on the first click of the button.
transmit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//data validation
///////////////////////
boolean valid = true;
if(((EditText)findViewById(R.id.drv_in)).getText().toString().equals("")) {
TX_FAIL_TEXT = "Missing Driver ID!";
showDialog(DIALOG_FAIL);
TX_FAIL_TEXT = "Transmission Failed!"; //reset the dialog fail text to default
valid = false;
}
Log.e("SMSDRVERR", ((EditText)findViewById(R.id.drv_in)).getText().toString());
if(custSpn.getSelectedItemPosition() == 0) {
TX_FAIL_TEXT = "Missing Customer Selection!";
showDialog(DIALOG_FAIL);
TX_FAIL_TEXT = "Transmission Failed!"; //reset the dialog fail text to default
valid = false;
}
if(prdSpn.getSelectedItemPosition() == 0) {
TX_FAIL_TEXT = "Missing Product Selection!";
showDialog(DIALOG_FAIL);
TX_FAIL_TEXT = "Transmission Failed!"; //reset the dialog fail text to default
valid = false;
}
if(((Cursor)prdSpn.getItemAtPosition(prdSpn.getSelectedItemPosition())).getString(prdSpn.getSelectedItemPosition()).contains("CAR") ||
((Cursor)prdSpn.getItemAtPosition(prdSpn.getSelectedItemPosition())).getString(prdSpn.getSelectedItemPosition()).contains("AUTO") ||
((Cursor)prdSpn.getItemAtPosition(prdSpn.getSelectedItemPosition())).getString(prdSpn.getSelectedItemPosition()).contains("TRUCK")
) {
//must have make, license# and 1vin
if(((EditText)findViewById(R.id.make_in)).getText().toString().equals("")) {
TX_FAIL_TEXT = "Vehicle Entry:\n Missing Make/Model!";
showDialog(DIALOG_FAIL);
TX_FAIL_TEXT = "Transmission Failed!"; //reset the dialog fail text to default
valid = false;
}
if(((EditText)findViewById(R.id.tag_in)).getText().toString().equals("")) {
TX_FAIL_TEXT = "Vehicle Entry:\n Missing Tag Number!";
showDialog(DIALOG_FAIL);
TX_FAIL_TEXT = "Transmission Failed!"; //reset the dialog fail text to default
valid = false;
}
if(((EditText)findViewById(R.id.vin1_in)).getText().toString().equals("") ||
((EditText)findViewById(R.id.vin2_in)).getText().toString().equals("") ||
((EditText)findViewById(R.id.vin3_in)).getText().toString().equals("") ||
((EditText)findViewById(R.id.vin4_in)).getText().toString().equals("") ||
((EditText)findViewById(R.id.vin5_in)).getText().toString().equals("") ||
((EditText)findViewById(R.id.vin6_in)).getText().toString().equals("") ||
((EditText)findViewById(R.id.vin7_in)).getText().toString().equals("") ||
((EditText)findViewById(R.id.vin8_in)).getText().toString().equals("")
) {
TX_FAIL_TEXT = "Vehicle Entry:\n Missing VIN Number!";
showDialog(DIALOG_FAIL);
TX_FAIL_TEXT = "Transmission Failed!"; //reset the dialog fail text to default
valid = false;
}
}
//Log.e("smsDRVERR",((EditText)smsActivity.this.findViewById(R.id.drv_in)).getText().toString());
//begin transmission
///////////////////////
if(valid) {
showDialog(DIALOG_TX_PROGRESS);
Thread t = new Thread(txRunnable);
t.start();
} else {
//do things if needed
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会将其作为评论发布,但它太长了......
我不认为问题是你所认为的那样。但是,我无法说出问题是什么,因为您还不清楚如何检测成功和/或失败。
让我们首先澄清诊断代码,以消除任何可能的歧义。我建议您将其更改
为:
这将消除日志中关于文本是否确实丢失的任何可能的歧义。 (这也使得代码更具可读性。)
I'd post this as a comment, but it'd be too long...
I don't think the problem is what you think it is. However, I can't say what the problem is, because you haven't been clear about how you're detecting success and/or failure.
Let's start by clarifying the diagnostic code, to remove any possible ambiguities. I'd suggest you change this:
to:
This will eliminate any possible ambiguity in the log about whether the text really was missing. (It also makes for more readable code.)
问题实际上出在 Dialog 对象上。有条件就好了。在 onClick 方法的开头,我添加了一个调用:
这会强制 Android 在下次调用时重建对话框。
编辑:为了将来的参考,有一种更优雅的方法可以使用 onPrepareDialog() 来执行此操作,但这个解决方案对我来说更容易。
the problem was actually with the Dialog objects. the conditional is fine. at the beginning of the onClick method, i added a call to:
this forces Android to rebuild the Dialog the next time it is called.
EDIT: for future reference, there is a more elegant way to do this using onPrepareDialog(), but this solution was easier for me.