android - 为 Spinner 创建一个 setError()

标签 android spinner

如何为 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/

相关文章:

使用 HTTPS 连接到 Web 服务时,Android SSL 握手失败

Android Spinner OnItemSelected 仅在用户交互时

java - 在外部类中使用微调器,因为所有其他 Activity 都需要

android - 微调器没有获得焦点

android - 如何将微调器中的文本放入文件?

android - 设置根据 Intent Extras 值选择的微调项

android - 如何提取或解压 .ab 文件(Android 备份文件)

java - 为什么 `exif.setAttribute(ExifInterface.TAG_ORIENTATION, String.valueOf(ExifInterface.ORIENTATION_ROTATE_270));` 什么也不做?

java - 无法弄清楚为什么 fragment 中没有显示列表

android - 以编程方式设置文本时,如何避免出现自动完成下拉菜单?