本文介绍了创建SETERROR()用于微调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建 SETERROR()(类似于一个的TextView / EditText上)的函数的微调?下面不工作:

我试图扩展微调类,并在构造函数中:

  ArrayAdapter<字符串> AA =新的ArrayAdapter<字符串>(的getContext()
                    android.R.layout.simple_spinner_item,android.R.id.text1,
                    项);
            aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            setAdapter(AA);
             电视=(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 方法:

 公共无效SETERROR(字符串str){
        如果(电视!= NULL)
            tv.setError(STR);
        如果(TV2!= NULL)
            tv2.setError(STR);
        如果(CTV!= NULL)
            ctv.setError(STR);
    }
 

解决方案

我不涉及创建一个额外的编辑领域的解决方案,但你需要有自己的 SpinnerAdapter 像往常一样。

请确保你至少有一个的TextView 在你使用的布局您的适配器 getView()(你通常有,反正)。

将以下函数添加到您的适配器(变化名称的TextView 的ID):

 公共无效SETERROR(视图V,CharSequence中){
  TextView的名称=(TextView中)v.findViewById(R.id.name);
  name.setError(多个);
}
 

致电 SETERROR()从C这样你的$ C $:

  YourAdapter适配器=(YourAdapter)spinner.getAdapter();
查看查看= spinner.getSelectedView();
adapter.setError(视图,getActivity()的getString(R.string.error_message)。);
 

基本上,与任何其他的控制,只有你把它的适配器上,你必须提供的观点也是如此。

这将显示在微调的错误图标,因为它与其他控件的情况。

How do you create the setError() (similar to that of a TextView/EditText) function for a Spinner? The following doesn't work:

I tried extending the Spinner class and in the constructor:

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 method:

    public void setError(String str) {
        if (tv != null)
            tv.setError(str);
        if(tv2!=null)
            tv2.setError(str);
        if (ctv != null)
            ctv.setError(str);
    }
解决方案

I have a solution that doesn't involve creating an extra edit field but you need your own SpinnerAdapter, as usual.

Make sure you have at least one TextView in the layout you use in your adapter's getView() (you normally have that, anyway).

Add the following function to your adapter (change name to the ID of your TextView):

public void setError(View v, CharSequence s) {
  TextView name = (TextView) v.findViewById(R.id.name);
  name.setError(s);
}

Call the setError() from your code this way:

YourAdapter adapter = (YourAdapter)spinner.getAdapter();
View view = spinner.getSelectedView();
adapter.setError(view, getActivity().getString(R.string.error_message));

Basically, as with any other control, only that you call it on your adapter and you have to provide the view as well.

This will display the error icon on the spinner as it is the case with other controls.

这篇关于创建SETERROR()用于微调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 04:17