我希望能够像这样调用代码,类似于在 TextView 上设置 setError
的方式:
spinner.setError("Error message");
但是,setError
仅适用于 EditText,不适用于 Spinner。
如果未选择微调器字段,我想通知用户。如何不使用 Toast 执行此类通知?
最佳答案
这个帖子中有一些解决方案Creating a setError() for the Spinner :
EdmundYeung99 的一个对我有用,无论您是否使用自己的适配器。 只需将以下代码放入您的验证函数中:
TextView errorText = (TextView)mySpinner.getSelectedView();
errorText.setError("");
errorText.setTextColor(Color.RED);//just to highlight that this is an error
errorText.setText("my actual error text");//changes the selected item text to this
但是,在进行验证时,请确保 Spinner 适配器中至少有一个值。如果没有,就像一个等待填充的空适配器,让你的适配器得到一个空字符串:
ArrayAdapter<String> adapter = new ArrayAdapter<>(context, android.R.layout.simple_spinner_item, new String[]{""});
mySpinner.setAdapter(adapter);
关于android - 如何为 Android 中的 Spinner 设置错误消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28235689/