如何使用Android的微调像一个下拉列表 [英] How to use Android Spinner like a drop-down list

查看:123
本文介绍了如何使用Android的微调像一个下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我花相当长的一段让我的头围绕Android的微调。几次失败的尝试实施后,阅读了许多问题部分相似的我自己,但<一后href="http://stackoverflow.com/questions/2373948/ignore-onitemselectedlistener-firing-on-create">without满意答案的,有的没有任何答案可言,例如: <一href="http://stackoverflow.com/questions/6788664/android-spinner-onitemselectedlistener-not-working-accurately">here和<一href="http://stackoverflow.com/questions/7043183/onitemselectedlistener-only-triggered-if-selection-actually-change">here,我终于在Android的一个微调并不意味着是同样的事情,作为一个从桌面应用程序下拉列表,或者在HTML中的选择。然而,我的应用程序(我猜所有的其他海报的问题是类似的应用程序)的需求是什么,就像一个下拉框,而不是像一个微调。

It's taken me quite a while to get my head around the Android Spinner. After several failed implementation attempts, and after reading many questions partially similar to my own but without satisfactory answers, and some without any answers at all, e.g. here and here, I finally get that a "spinner" in Android isn't meant to be the same thing as a "drop-down list" from desktop apps, or a select in HTML. However, what my app (and I'm guessing the apps of all the other posters whose questions are similar) needs is something that works like a drop-down box, not like a spinner.

我的两个问题是什么我首先考虑的是独特特性的OnItemSelectedListener(我见过这些在该网站不同的问题,但不是一个):

My two problems are with what I first considered to be idiosynchrasies the OnItemSelectedListener (I've seen these as separate questions on this site but not as one):

  1. 的初始选择第一个列表项的自动触发没有用户的相互作用。
  2. 当一个已经选择再次由用户选择的项目,将被忽略。

现在我意识到,当你想想看,这是有道理的,这种情况发生在微调的 - 它已经开始与选择的默认值,你旋转它只是改变这种状况值,而不是重新选择值 - <一个href="http://developer.android.com/reference/android/widget/AdapterView.OnItemSelectedListener.html">the文档居然说:这个回调函数,只有当新选择的位置距离pviously选定位置的$ P $不同。我也看到了<一个href="http://stackoverflow.com/questions/4206047/android-spinner-how-may-i-distinguish-users-actions-from-computers-actions">answers建议你设置一个标志忽略第一个自动选择 - 我想我可以忍受,如果没有别的办法

Now I realise that, when you think about it, it makes sense for this to happen on a spinner - it has to start with a default value selected, and you spin it only to change that value, not to "re-select" a value - the documentation actually says: "This callback is invoked only when the newly selected position is different from the previously selected position". And I've seen answers suggesting that you set up a flag to ignore the first automatic selection - I guess I could live with that if there's no other way.

但自从我真正想要的是它表现为一个下拉列表下拉列表应该(而且因为用户可以和应该期望),我需要的是什么的喜欢的一个微调框就像一个下拉菜单,像一个组合框。我不关心任何自动pre-选择(出现这种情况,而不会触发我的听众),我想知道的每次的选择,即使是相同的一个作为previously(毕竟,用户的选择的再次相同的项目)。

But since what I really want is a drop-down list which behaves as a drop-down list should (and as users can and should expect), what I need is something like a Spinner that behaves like a drop-down, like a combo-box. I don't care about any automatic pre-selection (that should happen without triggering my listener), and I want to know about every selection, even if it's the same one as previously (after all, the user selected the same item again).

所以......是有什么在Android中,能做到这一点,或者一些解决办法,使一个微调像一个下拉列表?如果没有的<​​em>是的像这样的在这个网站,我没有发现,而且它有一个满意的答案的问题,请让我知道(在这种情况下,我真诚地道歉,重复的问题)。

So... is there something in Android that can do that, or some workaround to make a Spinner behave like a drop-down list? If there is a question like this one on this site that I haven't found, and which has a satisfactory answer, please let me know (in which case I sincerely apologise for repeating the question).

推荐答案

+1大卫的回答。但是,这里有一个实施的建议,不涉及从源头(其中,顺便说一句,看起来完全像大卫发布<一个相同的复制粘贴code href="http://$c$csearch.google.com/$c$csearch#uX1GffpyOZk/core/java/android/widget/AdapterView.java">in 2.3以及):

+1 to David's answer. However, here's an implementation suggestion that does not involve copy-pasting code from the source (which, by the way, looks exactly the same as David posted in 2.3 as well):

@Override
void setSelectionInt(int position, boolean animate) {
    mOldSelectedPosition = INVALID_POSITION;
    super.setSelectionInt(position, animate);
}

这样你会欺骗父类的方法,以为这是一个新的岗位上的时间。

This way you'll trick the parent method into thinking it's a new position every time.

另外,你可以尝试设置的位置的微调被点击时无效,并设置它放回<一href="http://developer.android.com/reference/android/widget/AdapterView.OnItemSelectedListener.html"><$c$c>onNothingSelected.这是不是很好,因为用户不会看到选择什么样的项目,而该对话框了。

Alternatively, you could try setting the position to invalid when the spinner is clicked and setting it back in onNothingSelected. This is not as nice, because the user will not see what item is selected while the dialog is up.

这篇关于如何使用Android的微调像一个下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆