Creating a setError() for the Spinner

匿名 (未验证) 提交于 2019-12-03 01:51:02

问题:

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 aa = new ArrayAdapter(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);     }

回答1:

Similar to @Gábor's solution, but I didn't need to create my own adapter. I just call the following code in my validate function (i.e. on submit button clicked)

        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


回答2:

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.



回答3:

Using a hidden TextView to get a pop-up message to appear

This solution involves adding an additional hidden textbox just below the spinner just at the right position to allow the TextView's error dialog to show, whilst also using the TextView set on the spinner's layout XML to allow the red (!) icon to be displayed. So in effect, two textviews are used -- one for the icon, and another (hidden) one to allow the error dialog.

This is what it looks like when not in an error state (use SetError(null)):

This is what it looks like when there is an error (use SetError("my error text, ideally from a resource!")):

Here is the excerpt of the spinner's layout XML. There is a RelativeLayout used to ensure that the TextView is as close as possible to the spinner, and has just enough paddingRight to ensure that the arrow on the message dialog is aligned just under the red error (!) icon. The hidden (fake) TextView is positioned relative to the Spinner.

                                       

Note: @drawable/selector_listview defined outside the scope of this solution. See example here of how to get this to work, as it's off topic for this answer.

Here is the code to make it work. Just call the SetError(errMsg) either with null to clear the error, or with text to set it in an error state.

/**  * When a errorMessage is specified, pops up an error window with the message  * text, and creates an error icon in the secondary unit spinner. Error cleared through passing  * in a null string.  * @param errorMessage Error message to display, or null to clear.  */ public void SetError(String errorMessage) {     View view = spnMySpinner.getSelectedView();      // Set TextView in Secondary Unit spinner to be in error so that red (!) icon     // appears, and then shake control if in error     TextView tvListItem   
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!