如何将“提交”选项灰显当特定的 EditText 框为空时按钮?
关于我正在创建的 Android 应用程序:
我有三个 EditText 框需要填充数字/字符串。我有一个提交按钮,它将开始一系列计算。
如果任何框为空并按下提交,应用程序就会崩溃。我尝试用 try-catch 语句来做到这一点,但没有成功。我只是想禁用该按钮,直到三个框都有数字。我知道有一种方法可以 setEnabled(false)
我想?或者有更好的方法吗?这会使按钮变灰吗?或者这是一个与 setEnabled 无关的函数?
Regarding an Android app I am creating:
I have three EditText boxes that need to be filled with numbers/strings. I have a submit button that will start a series of calculations.
IF any box is empty and submit is pressed, the app crashes. I have tried to do this with try-catch statement, but it is not working out. I simply want to disable the button until three boxes have numbers. I know there is a way to setEnabled(false)
I think? Or is there a better way? Will this grey out the button? Or is that an unrelated function to setEnabled?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试这个解决方案。
Try this solution.
您应该在 XML 中默认禁用该按钮,以便用户不会意外点击该按钮。之后,您需要查看您的字段并确保所有字段在提交之前都有数据。
您可以这样做,或者您可以提交对所有字段进行快速检查并确保它们都不等于“”。
基本上看起来像这样(如果你想忽略隐藏按钮并在检查后处理处理)
否则你可以像上面提到的海报那样有一个“观察者”。
You should disable the button by default in the XML so that the user cannot hit the button by accident. After this you need to look at your fields and insure all of them have data before the submission.
You could do this, or you could just have submit run a quick check on all the fields and insure none of them are equal to "".
Basically look like this (If you want to ignore hiding the button and just handle processing after the check)
Otherwise you can just have a "watcher" like the above poster mentioned.
您也可以使用此支票
You can also use this check
您好,我已经尝试了上面的代码,并将函数更改为下面给出的,以使其正常工作。
Hi i have tried above code and changed the function to given below for it to work.