如何将 Spinner 默认值设置为 null?
我正在尝试加载一个没有选定值的微调器。一旦用户选择了一个值,它就会将他们带到另一个页面。
事实证明这是一个问题,因为目前,页面只是在用户做出选择之前立即加载。
我的 spinner 类的设置方式与 Google 的相同: http://developer .android.com/resources/tutorials/views/hello-spinner.html
所以基本上,是否有可能有一个没有选择任何内容的微调器,因为目前它加载了我的字符串数组中的第一项。
I'm trying to get a Spinner to load up with no selected value. Once the user selects a value it then takes them to another page.
This is proving to be a problem because at present, the page just loads straight away before the user gets a choice to choose.
My spinner class is set up the same way as Google's: http://developer.android.com/resources/tutorials/views/hello-spinner.html
So basically, is it possible have a spinner that loads with nothing selected because at present, it loads the first item in my string array.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
仅在没有数据的情况下才加载任何内容。如果
SpinnerAdapter
中有 1 个以上项目,则Spinner
将始终有一个选择。Spinner
并非设计为命令小部件。用户不会期望在Spinner
中进行选择来启动活动。请考虑使用其他内容,例如ListView
或GridView
,而不是Spinner
。编辑
顺便说一句,我忘了提及 - 您始终可以在适配器中添加一个表示“无选择”的额外条目,并使其成为
Spinner
中的初始选定项目。Only if there is no data. If you have 1+ items in the
SpinnerAdapter
, theSpinner
will always have a selection.Spinners
are not designed to be command widgets. Users will not expect a selection in aSpinner
to start an activity. Please consider using something else, like aListView
orGridView
, instead of aSpinner
.EDIT
BTW, I forgot to mention -- you can always put an extra entry in your adapter that represents "no selection", and make it the initial selected item in the
Spinner
.或者,您可以重写微调器适配器,并在 getView 方法中为位置 0 提供一个空视图,并在
getDropDownView
方法中提供高度为 0dp 的视图。这样,您就有了一个初始文本,例如“选择一个选项...”,该文本在首次加载微调器时显示,但它不是用户可以选择的选项(从技术上讲是这样,但因为高度为 0 ,他们看不到)。
Alternatively, you could override your spinner adapter, and provide an empty view for position 0 in your getView method, and a view with 0dp height in the
getDropDownView
method.This way, you have an initial text such as "Select an Option..." that shows up when the spinner is first loaded, but it is not an option for the user to choose (technically it is, but because the height is 0, they can't see it).
这是 Paul Bourdeaux 的想法的完整实现,即在
中返回一个特殊的初始视图(或空视图) getView()
用于位置 0。它对我有用并且相对简单。如果您已经有适用于 Spinner 的自定义适配器,您可能会考虑使用此方法,尤其是。 (在我的例子中,我使用自定义适配器来轻松自定义项目的布局,每个项目都有几个 TextView。)
适配器将是这样的:
就是这样。请注意,如果您在 Spinner 中使用
OnItemSelectedListener
,则在onItemSelected()
中,您还必须调整position
以将默认项目放入帐户,例如:This is a complete implementation of Paul Bourdeaux's idea, namely returning a special initial view (or an empty view) in
getView()
for position 0.It works for me and is relatively straightforward. You might consider this approach especially if you already have a custom adapter for your Spinner. (In my case, I was using custom adapter in order to easily customise the layout of the items, each item having a couple of TextViews.)
The adapter would be something along these lines:
That's it. Note that if you use a
OnItemSelectedListener
with your Spinner, inonItemSelected()
you'd also have to adjustposition
to take the default item into account, for example:就我而言,虽然微调器中显示尺寸“2”,但在完成某些选择之前什么也不会发生!
我有一个 xml 文件 (data_sizes.xml),其中列出了所有微调器值。
在 main.xml 文件中: Spinner 元素
然后在我的 java 代码中添加:
在我的活动中:
在 public void 函数中声明 - initControls(): 定义
我的 spinner 监听器:
/* Spinner Listener */
In my case, although size '2' is displayed in the spinner, nothing happens till some selection is done!
I have an xml file (data_sizes.xml) which lists all the spinner values.
In main.xml file: Spinner element
Then in my java code, I added:
In my activity: Declaration
In a public void function - initControls(): Definition
My spinner listener:
/* Spinner Listener */
使用如下所示的自定义微调器布局:
在活动中:
Using a custom spinner layout like this:
In the activity:
合并此:
使用此答案:
Merge this:
With this answer:
您可以将数组中的第一个单元格设置为空({"","some","some",...})
如果位置为 0,则不执行任何操作;
you can put the first cell in your array to be empty({"","some","some",...})
and do nothing if the position is 0;
我的解决方案,如果您有一个 TextView 作为微调器的每一行:
My solution, in case you have a TextView as each row of the spinner:
我假设您希望有一个带有第一个空的不可见项目的
Spinner
(这是Spinner
的一个奇怪功能,它无法在不选择项目的情况下显示列表)。您应该添加一个包含数据的类:这是适配器。
创建:
添加项目:
当您使用
fill()
命令将项目加载到Spinner
时,您应该知道索引也会递增。因此,如果您想选择第三项,现在应该选择第四项:spinner?.setSelection(index + 1)
I assume that you want to have a
Spinner
with first empty invisible item (that is a strange feature ofSpinner
that cannot show a list without selecting an item). You should add a class that will contain data:This is the adapter.
To create:
To add items:
When you load items to your
Spinner
with thatfill()
command, you should know, that indices are also incremented. So if you wish to select 3rd item, you should now select 4th:spinner?.setSelection(index + 1)
根据@Jonik 的回答,我已经创建了 ArrayAdapter 的功能齐全的扩展
Base on @Jonik answer I have created fully functional extension to ArrayAdapter
解释 - 我们可以将 arrayList 的第一个参数中的虚拟值发送到 spinner 适配器。然后将 arrayList 中虚拟值的位置(0 或 arrayList 之间的任何位置)发送到 SpinnerAdapter 构造函数。
然后在微调器适配器类中以与我相同的方式隐藏它并完成。
}
Explanation- We can send a Dummy Value in first Parameter of our arrayList to spinner Adapter. Then send position(0 or any in between of arrayList) of Our dummy value in arrayList to SpinnerAdapter constructor .
then in spinner adapter class hide it in same way as i did and its done.
}