如何为 Spinner
创建 setError()
(类似于 TextView/EditText
)函数?以下不起作用:
我尝试在构造函数中扩展 Spinner 类:
ArrayAdapter<String> aa = new ArrayAdapter<String>(getContext(),
android.R.layout.simple_spinner_item, android.R.id.text1,
items);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
setAdapter(aa);
tv = (TextView) findViewById(android.R.id.text1);
// types_layout_list_tv
ctv = (CheckedTextView) aa.getDropDownView(1, null, null);
tv2 = (TextView) aa.getView(1, null, null);
setError
方法:
public void setError(String str) {
if (tv != null)
tv.setError(str);
if(tv2!=null)
tv2.setError(str);
if (ctv != null)
ctv.setError(str);
}
最佳答案
类似于@Gábor 的解决方案,但我不需要创建自己的适配器。我只是在我的验证函数中调用以下代码(即点击提交按钮)
TextView errorText = (TextView)mySpinner.getSelectedView();
errorText.setError("anything here, just to add the icon");
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
关于android - 为 Spinner 创建一个 setError(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3749971/