微调:获得国家或得到通知打开时 [英] Spinner: get state or get notified when opens
问题描述
是否有可能知道是否微调
是打开还是关闭?它甚至会更好,如果有对纱厂某种onOpenListener的。
Is it possible to know whether a Spinner
is open or closed? It would even be better if there was some sort of onOpenListener for Spinners.
我使用的是OnItemSelectedListener这样的尝试:
I've tried using an OnItemSelectedListener like this:
spinnerType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
executeSomething();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
Log.d("nothing" , "selected");
}
});
我知道,如果事情是选择的窗口将关闭(在executeSomething())。但是,我没有得到通知,如果我点击对话,这也关闭了微调之外
I can know that the window will close if something is selected (in executeSomething()). But I don't get notified if I click outside of the Dialog, which also closes the spinner
推荐答案
,以观察这些事件的另一个选择是延长微调
类,并使用它的方法之一( performClick
将触发其对话框/弹出),通过监测活动的焦点
。这应该给你想要关闭的事件所有可能整理的可能性(对于对话框或下拉列表模式):
Another option to watch for those events is to extend the Spinner
class and use one of its methods(performClick
which will trigger its dialog/popup) followed by monitoring the focus of the Activity
. This should give you the wanted closed event for all the possible finishing possibilities(for either the dialog or dropdown mode):
public interface OnSpinnerEventsListener {
void onSpinnerOpened();
void onSpinnerClosed();
}
自定义微调
类:
public static class CustomSpinner extends Spinner {
private OnSpinnerEventsListener mListener;
private boolean mOpenInitiated = false;
// the Spinner constructors
@Override
public boolean performClick() {
// register that the Spinner was opened so we have a status
// indicator for the activity(which may lose focus for some other
// reasons)
mOpenInitiated = true;
if (mListener != null) {
mListener.onSpinnerOpened();
}
return super.performClick();
}
public void setSpinnerEventsListener(
OnSpinnerEventsListener onSpinnerEventsListener) {
mListener = onSpinnerEventsListener;
}
/**
* Propagate the closed Spinner event to the listener from outside.
*/
public void performClosedEvent() {
mOpenInitiated = false;
if (mListener != null) {
mListener.onSpinnerClosed();
}
}
/**
* A boolean flag indicating that the Spinner triggered an open event.
*
* @return true for opened Spinner
*/
public boolean hasBeenOpened() {
return mOpenInitiated;
}
}
和的活动的重点手表
:
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
// mSpin is our custom Spinner
if (mSpin.hasBeenOpened() && hasFocus) {
mSpin.performClosedEvent();
}
}
这篇关于微调:获得国家或得到通知打开时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!