简体   繁体   English

Android - 如何判断是某人按下了SPINNER但未更改其中的可见项目

[英]Android - How to tell is someone has pressed a SPINNER but not changed the visible item in it

I have a spinner, which mostly works. 我有一个旋转器,主要工作。 If a user selects one of the items in it, the 'onItemSelected' routine catches it just fine. 如果用户选择其中一个项目,'onItemSelected'例程就可以捕获它。

But if a user clicks the same spinner, but does not change from the already visible item that it's currently displaying, the 'onItemSelected' routine just ignores it, and the logs show:- 但是,如果用户点击相同的微调器,但没有从当前显示的已经可见的项目进行更改,则“onItemSelected”例程会忽略它,并且日志显示: -

WARN/InputManagerService(577): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@437948b0 WARN / InputManagerService(577):窗口已经聚焦,忽略焦点增益:com.android.internal.view.IInputMethodClient$Stub$Proxy@437948b0

I there anyway to capture someone doing this? 无论如何我在那里捕捉到这样的人? The idea is that my spinner contains a list of names, and when a user selects one from the spinner, it gets added to a listview. 我的想法是我的微调器包含一个名称列表,当用户从微调器中选择一个时,它会被添加到列表视图中。

I could just add another button to get the name from the spinner, but, screen-space is already lacking and I'd rather not add anymore content. 我可以添加另一个按钮来从微调器获取名称,但是,屏幕空间已经缺少,我宁愿不再添加内容。

This problem could be solved with overriding Spinner.onClick() , but this is buggy from android 3.0 and above ( Spinner.onClick() is called on 2.2 device, not called on 3.0 device ). 这个问题可以通过覆盖Spinner.onClick()来解决,但这是从android 3.0及以上版本( 在2.2设备上调用Spinner.onClick() ,而不是在3.0设备上调用 )。

So the best solution is the AlertDialog.Builder as @adamp said. 所以最好的解决方案是AlertDialog.Builder正如@adamp所说。

AlertDialog.Builder builder = new Builder(this);
// maybe get from resources 
CharSequence titles[] = getResources().getTextArray(R.array.titles);
builder.setItems(titles, new DialogInterface.OnClickListener() {
  @Override
  public void onClick(DialogInterface dialog, int which) {
    // add your code here               
  }
});
builder.create().show();

It sounds like you don't actually want a Spinner, you're just using it for its popup dialog. 听起来你实际上并不想要一个Spinner,你只是将它用于弹出对话框。 You can create your own simple popup dialog like Spinner uses (as the result of an 'Add' button click in your UI perhaps?) by using AlertDialog.Builder . 您可以使用AlertDialog.Builder创建自己的简单弹出对话框,如Spinner使用的(可能是在用户界面中点击“添加”按钮的结果?)。 Spinner uses setSingleChoiceItems followed by show on an AlertDialog.Builder to present its choices to the user. Spinner在AlertDialog.Builder上使用setSingleChoiceItems后跟show来向用户显示其选择。

Never used the spinner but wouldn't it be possible to reset the spinners position in the onItemSelected routine? 从未使用过微调器,但是不能在onItemSelected例程中重置微调器位置吗? To some kind of null value that says "Please choose" or something. 对某种说“请选择”或其他东西的空值。

Have you looked at setOnItemClickListener ? 你看过setOnItemClickListener了吗? If you implement your own AdapterView.OnItemClickListener and pass it in as the argument to mySpinner.setOnItemClickListener method you'll be able to get ahold of the position of the selected item inside the onItemClick method 如果您实现自己的AdapterView.OnItemClickListener并把它作为参数来mySpinner.setOnItemClickListener方法,你就可以得到阿霍德所选项目的内部位置的onItemClick方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM