gpt4 book ai didi

android - 微调器 : get state or get notified when opens

转载 作者:IT王子 更新时间:2023-10-28 23:53:42 27 4
gpt4 key购买 nike

是否有可能知道 Spinner 是打开还是关闭?如果 Spinner 有某种 onOpenListener 就更好了。

我尝试过像这样使用 OnItemSelectedListener:

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() 中),窗口将关闭。但是如果我在对话框之外单击,我不会收到通知,这也会关闭微调器

最佳答案

监视这些事件的另一个选项是扩展 Spinner 类并使用其方法之一(performClick() 将触发其对话框/弹出窗口),然后是监视持有此自定义 Spinner 的窗口的焦点。这应该为您提供所有可能的完成可能性(对于对话框或下拉模式)所需的关闭事件。

自定义Spinner类:

public class CustomSpinner extends Spinner {

/**
* An interface which a client of this Spinner could use to receive
* open/closed events for this Spinner.
*/
public interface OnSpinnerEventsListener {

/**
* Callback triggered when the spinner was opened.
*/
void onSpinnerOpened(Spinner spinner);

/**
* Callback triggered when the spinner was closed.
*/
void onSpinnerClosed(Spinner spinner);

}

private OnSpinnerEventsListener mListener;
private boolean mOpenInitiated = false;

// implement the Spinner constructors that you need

@Override
public boolean performClick() {
// register that the Spinner was opened so we have a status
// indicator for when the container holding this Spinner may lose focus
mOpenInitiated = true;
if (mListener != null) {
mListener.onSpinnerOpened(this);
}
return super.performClick();
}

@Override
public void onWindowFocusChanged (boolean hasFocus) {
if (hasBeenOpened() && hasFocus) {
performClosedEvent();
}
}

/**
* Register the listener which will listen for events.
*/
public void setSpinnerEventsListener(
OnSpinnerEventsListener onSpinnerEventsListener) {
mListener = onSpinnerEventsListener;
}

/**
* Propagate the closed Spinner event to the listener from outside if needed.
*/
public void performClosedEvent() {
mOpenInitiated = false;
if (mListener != null) {
mListener.onSpinnerClosed(this);
}
}

/**
* A boolean flag indicating that the Spinner triggered an open event.
*
* @return true for opened Spinner
*/
public boolean hasBeenOpened() {
return mOpenInitiated;
}

}

关于android - 微调器 : get state or get notified when opens,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18447063/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com