Android 微调器关闭
我有一个带有微调器的活动,我想知道如果用户已打开微调器,是否可以以编程方式关闭微调器。
整个故事是,在后台我正在一个单独的线程上运行一个进程。该流程完成后,我在主活动上调用处理程序,并根据结果执行一些任务。然后我想关闭微调器,如果用户打开了它。
微调器位于 main.xml 布局中:
<Spinner android:id="@+id/birthPlaceSpinner" android:layout_weight="1"
android:layout_height="wrap_content" android:prompt="@string/select"
android:layout_width="fill_parent" />
这是处理程序:
private class BirthplaceChangedHandler extends Handler {
@Override
public void handleMessage(Message msg) {
String placeFilterStr = birthPlaceFilterText.getText().toString();
if ("".equals(placeFilterStr) || placeFilterStr == null || validNewAddresses.isEmpty()) {
birthPlaceSpinner.setEnabled(false);
hideGeoLocationInformation();
} else {
birthPlaceSpinner.setEnabled(true);
}
adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.multiline_spinner_dropdown_item, validNewAddressesStr)
birthPlaceSpinner.setAdapter(adapter);
}
}
干杯!
I have an activity with a spinner, and I was wondering if it is possible to close the spinner programmatically, if the user has opened it.
The whole story is that in the background I am running a process on a separate thread. When the process has finished, I invoke a Handler on the main activity and, depending on the outcome, I perform some tasks. It is then that I want to close the spinner, it the user has opened it.
The spinner is in the main.xml layout:
<Spinner android:id="@+id/birthPlaceSpinner" android:layout_weight="1"
android:layout_height="wrap_content" android:prompt="@string/select"
android:layout_width="fill_parent" />
and this is the Handler:
private class BirthplaceChangedHandler extends Handler {
@Override
public void handleMessage(Message msg) {
String placeFilterStr = birthPlaceFilterText.getText().toString();
if ("".equals(placeFilterStr) || placeFilterStr == null || validNewAddresses.isEmpty()) {
birthPlaceSpinner.setEnabled(false);
hideGeoLocationInformation();
} else {
birthPlaceSpinner.setEnabled(true);
}
adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.multiline_spinner_dropdown_item, validNewAddressesStr)
birthPlaceSpinner.setAdapter(adapter);
}
}
Cheers!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
这对我有用:
以及点击侦听器:
This works for me:
and the click listener:
嗯,它比我想象的有点复杂。
我在这里添加逐步详细信息。尝试遵循它。我能够在 api 级别 10 中实现这一目标。
此解决方案假设您应该在用户单击“主页”按钮时以编程方式关闭提示对话框,或者如果您必须在没有用户交互的情况下移动到下一个活动
第一步是通过扩展 Spinner 类来创建自定义 Spinner。
比方说,我在 com.bts.sampleapp 包中创建了一个名为 CustomSpinner 的类,
我的 CustomSpinner 类如下所示,
现在在您的 Xml 文件中,用此替换 Spinner 元素自定义微调器,
下一步是在您的 Activity 类中初始化并设置适配器到此微调器,
最后一步是当用户单击 HomeButton 或当 Activity 移动到后台时关闭对话框。为此,我们像这样重写 onPause(),
现在在 onPause() 中调用方法
spin.onDetachedFromWindow();
,该方法会为您关闭提示对话框。话虽这么说,从 Activity 中的任何位置调用
spin.onDetachedFromWindow();
应该可以帮助您以编程方式关闭微调器。因此,如果这是您想要的,请删除onpause()
。Well its a little complicated than I thought.
I am adding the step by step details here. Try to follow it. I was able to achieve this in api level 10.
And this solution assumes that you are supposed to close the prompt dialog programatically when the user clicks on Home Button or If you had to move to next activity without user interaction
The first step is to create a Custom Spinner by extending Spinner Class.
Let's say, I have created a class called CustomSpinner in the package com.bts.sampleapp
My CustomSpinner class looks like this,
Now in your Xml file, replace Spinner element by this custom spinner,
The next step is to initialize and set adapter to this spinner in your Activity class,
The final step is to close the dialog when the user clicks on HomeButton or When the Activity moves to background. To do this, we override the onPause() like this,
Now within the onPause() call the method
spin.onDetachedFromWindow();
which does the job of closing the prompt dialog for you.Now that being said, calling
spin.onDetachedFromWindow();
from anywhere in your Activity should help you to close the spinner programatically. So if this is what you want, then remove theonpause()
.我没有找到实现此目的的方法 -
Spinner
上没有方法可以关闭它。Spinner
的“打开”部分是 Android 1.x 和 2.x 上的AlertDialog
,我不完全确定在使用时它是如何在 Honeycomb 上实现的全息主题。唯一的解决方法是克隆
Spinner
的源代码并自行添加一些代码以关闭该对话框。但是,同样,它无法在 Honeycomb 或更高版本上运行,除非您也可以看到并克隆该代码。除此之外,我认为你想要的是糟糕的用户体验。如果用户打开
Spinner
,他们很可能会主动检查Spinner
的内容并做出选择。从他们的手指下把它拉出来最多只会让他们感到困惑。请考虑另一种方法。另外,除非您知道为什么使用
getApplicationContext()
,否则请勿使用getApplicationContext()
。创建ArrayAdapter
时,您不需要甚至不需要getApplicationContext()
。I don't see a way to accomplish that -- there is no method on
Spinner
to close it. The "open" part of aSpinner
is anAlertDialog
on Android 1.x and 2.x, and I'm not completely sure how it is implemented on Honeycomb when using the holographic theme.The only workaround would be to clone the source for
Spinner
and add in some code yourself to dismiss the dialog. But, again, it would not work on Honeycomb or higher until you can see and clone that code as well.Beyond that, I would think that what you want is poor UX. If the user opened the
Spinner
, they are most likely actively examining theSpinner
's contents and making a selection. Yanking that out from under their finger will confuse them, at best. Please consider an alternative approach.Also, don't use
getApplicationContext()
unless you know why you are usinggetApplicationContext()
. You do not need or even wantgetApplicationContext()
when creating anArrayAdapter
.我认为你应该放弃使用
Spinner
,而是使用ImageView
和 帧动画(即
)来创建您自己的微调器。您只需将ImageView
的背景设置为您的帧动画可绘制对象。然后您可以轻松地执行这样的操作来开始并停止它。
I think you should scrap your use of
Spinner
and instead use anImageView
with a Frame Animation (i.e.<animation-list>
) to create your own spinner. You just set theImageView
's background to be your Frame Animation drawable.Then you can easily do something like this to start and stop it.
您想从任何地方关闭旋转器。 BACK 按下的按键注入是一个很好的解决方案,但是,在这里您将立即关闭所有视图。
setPressed(false) 怎么样?
关联:
同时单击组视图中的所有旋转器中的两个时关闭旋转器下拉列表
否则:
尝试使 Spinner
focusable
和focusableInTouchMode
并使用clearFocus()
在它上面。尝试使用 requestFocus() 方法将焦点集中在其下方的视图上。
检查微调器下拉菜单是否关闭
You would like to close your spinners from anywhere. Key injection for BACK pressed is the good solution but, here you are closing all the views at once.
How about setPressed(false)?
Link:
Close Spinners dropdown when two among all in a groupview are clicked simultaneously
Otherwise:
Try to make the Spinner
focusable
andfocusableInTouchMode
, and useclearFocus()
on it. Try to focus on the view below it using
requestFocus()
method.Check if the spinner drop-down closes
使用
clearFocus()
以编程方式关闭微调器Use the
clearFocus()
to close the spinner programitically在代码中添加clearfocus()。
在xml中使用透明背景
Add clearfocus() in code.
Use transparent background in xml
科尔廷反射
Koltin Reflection