如果在点击按钮处理输入后其中一个或多个字段留空,我想知道如何验证我的 4 个编辑文本字段。我已经搜索了很多解决方案,比如使用 toast,但它认为它不适合多个 edittext 字段和使用 textwatchers。我希望该应用程序显示一条弹出消息或警告对话框,提示“请填写必填字段。”
如有任何帮助,我们将不胜感激。
最佳答案
您可以使用以下常用函数来检查编辑文本的 Null 值:
public static boolean m_isError; public static void checkEntryForEmptyValue(EditText p_editText, String p_nullMsg) { if (p_editText != null || p_nullMsg != null) { // use trim() while checking for blank values if ((p_editText.getText().toString().trim().equalsIgnoreCase("")) || (p_editText.getText().toString().trim().length() <= 0)) { m_isError = true; p_editText.setError(p_nullMsg); p_editText.requestFocus(); } } } }
在您的按钮点击监听器中使用以下函数:
CommonUtil.m_isError = false; CommonUtil.checkEntryForEmptyValue(edittext,getResources(). getString(R.string.MessageEmpty)); if (!CustomValidator.m_isError) { Toast.makeText(getApplicationContext(),"Success", Toast.LENGTH_SHORT).show(); } else { //Your dialog with the error messages. }
关于java - 如何使用弹出消息或警报对话框验证多个编辑文本字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14393442/