Android - Spinner:如何在 OnItemSelectedListener 中区分用户的操作和计算机的操作

发布于 2024-10-02 23:16:59 字数 634 浏览 0 评论 0原文

我在管理旋转器时遇到了麻烦,我可以请求你的帮助吗?

我有一个带有适配器的旋转器。 开始活动时,我使用值列表初始化微调器。 然后,我强制所选值成为我管理的对象中使用的值。

屏幕初始化后: 当用户在微调器中选择一个值时,根据选择的值,我可能会继续(或不)另一个活动,让用户选择补充且必要的值。 如果用户“取消”第二个活动,我想将微调器回滚到之前选择的值,并取消同时进行的一些操作。 如果用户转到第二个活动的末尾,一切都很好,我只想使用第二个活动中选择的数据刷新微调器显示(我重载适配器中的 getView 方法来执行此操作)。

总的来说,我可以轻松地完成所有这些,但是,当我在活动开始时强制在微调器中选择值时,或者当通过“取消”从第二个活动返回时,更改值事件将被捕获,并且第二个活动将被捕获。活动被触发(用户根本没有单击任何内容)。

仅当微调器中所选值的更改是由于用户的手动操作所致时,您如何允许启动第二个活动,并防止在微调器的值发生更改时启动相同的第二个活动“代码 ”?

我尝试了许多解决方案,例如在适配器中设置一个布尔值,告诉适配器是否会因“代码中”操作而引发下一个事件。 或者也在适配器中放入一个布尔值,以告知适配器是否已初始化自身,并且我在第一个更改捕获事件上强制该布尔值为 true。 但没有什么能真正发挥作用。

感谢您的帮助。

奥利弗

I'm in a trouble managing a spinner, so may I ask for your help ?

I have a spinner with its adapter.
I initialize the spinner with a list of values when starting my activity.
Then I force the selected value to be the one used in the object that I manage.

Once the screen is initialized :
When the user selects a value in the spinner, according to the selected value, I may continue (or not) to another activity for let the user choose a complementary and necessary value.
If the user "cancels" this second activity, I want to rollback the spinner to its previous selected value, and cancel some actions made in the meantime.
If the user goes to the end of the second activity, everything is fine and I want juste to refresh the spinner display with the datas selected in the second activity (I overload the getView method in the adapter to do this).

Overall, I can easily do all of this, however, when I force the selected value in the spinner at the begining of my activity, or whene returning back from the second activity by "Cancel", the change value event is catched and the second activity is triggered (the user did not click anything at all).

How would you allow the second activity to be lauched only if the change of the selected value in the spinner is due to a manual action from the user, and prevent that same second activity to be launched when the value of spinner is changed "in the code "?

I tried many solutions, as setting a boolean into the adapter that tells if the next event will be raised because of an "in the code" action.
Or also putting a boolean in the adapter that tells if the adapter has initialised itself, and I force that boolean to true on the forst change catched event.
But nothing that really works fine.

Thank you for your help.

Oliver

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寻找我们的幸福 2024-10-09 23:16:59

我总是用布尔标志来解决这个问题,它一点也不漂亮,但如果你仔细考虑的话它会起作用。

这个想法或多或少是,创建一个全局 usable 布尔值并用 false 初始化,在 onSelectedItemListener() 中使用该布尔值来选择是否触发操作,需要记住的重要一点是,在计算机第一次自动选择它后将其设置为 true,并在 onResume() 方法中将其重置为 false。

这并不完美,但应该可行。

编辑:

bool spinnerUsable1;
bool spinnerUsable2;
int positionSpinner;

public void onCreate(Bundle savedInstanceState){
   spinnerUsable1 = false;
   spinnerUsable2 = true;
   if(savedInstanceState != null){
      positionSpinner = savedInstanceState.getInt("posSpinner");
      if(positionSpinner != 0) spinnerUsable2 = false;
   }

   //Declare your spinner, set the on item selected lister
   spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                boolean spinnerUsable = (spinnerUsable1 && spinnerUsable2);
                if (!spinnerUsable1) {
                    spinnerUsable1 = true;
                } else if (!spinnerUsable2) {
                    spinnerUsable2 = true;
                }
                if (spinnerUsable) {
                    //Action;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // Nothing
            }
        });
}

像这样的东西应该有效。

I've always solved that issue with boolean flags, it isnt pretty at all, but it works if you think it through.

The idea is more or less, create a global usable boolean and init with false, in the onSelectedItemListener() use that boolean to choose wether or not to trigger the action, the important thing to remember is to set it to true after the computer has selected it the first time automatically, and reset it to false in the onResume() method.

This isnt perfect but it should work.

Edit:

bool spinnerUsable1;
bool spinnerUsable2;
int positionSpinner;

public void onCreate(Bundle savedInstanceState){
   spinnerUsable1 = false;
   spinnerUsable2 = true;
   if(savedInstanceState != null){
      positionSpinner = savedInstanceState.getInt("posSpinner");
      if(positionSpinner != 0) spinnerUsable2 = false;
   }

   //Declare your spinner, set the on item selected lister
   spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                boolean spinnerUsable = (spinnerUsable1 && spinnerUsable2);
                if (!spinnerUsable1) {
                    spinnerUsable1 = true;
                } else if (!spinnerUsable2) {
                    spinnerUsable2 = true;
                }
                if (spinnerUsable) {
                    //Action;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // Nothing
            }
        });
}

Something like this should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文