Android的 - 微调:我怎么可以区分计算机的操作用户的行为在OnItemSelectedListener行为、操作、我怎么、计算机

2023-09-08 09:48:46 作者:离开以后

我在闹管理微调,所以我可以找你帮忙?

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.

当屏幕被初始化: 当用户选择在所说的纺丝器的值,根据所选择的值,我可以继续(或不)到另一个活动让用户选择一个补充和必要值。 如果用户取消本次活动,我想回滚微调到previous选择的值,并取消在此期间取得了一些行动。 如果用户进入到第二个活动的结束,一切都很好,我想探微刷新微调显示在第二活动选择的DATAS(我在适配器超负荷getView方法来做到这一点)。

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).

总的来说,我可以很容易但做到这一切,当我强迫我的活动开始时的微调选定的值,或whene被取消,从第二个活动返回回来,变化值事件被逮住而第二项活动被触发(用户没有点击在所有的东西)。

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).

你如何允许第二活动待lauched仅当在所说的纺丝器的选定值的变化是由于来自用户的手动操作,和prevent该相同的第二活性被发射时的值微调被改变,在code?

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 "?

我试了很多的解决方案,如设置一个布尔成告诉,因为一个在code的行动,如果下一个事件将提高适配器。 或者,也把在告诉适配器是否已初始化本身适配器一个布尔值,我强制该布尔为true的福斯特变化逮住事件。 但没有真正运行良好。

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.

感谢您的帮助。

奥利弗

推荐答案

我总是解决了这个问题,布尔标志,它不是pretty的所有,但它的工作原理通过,如果你认为它。

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

我们的想法是多还是少,创建一个全球性的可用的布尔和init用的假的,在onSelectedItemListener()使用布尔选择羯羊或不触发行动,要记住最重要的是将其设置为true后,计算机自动选择它的第一次,并重置为假,在onResume()方法。

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.

编辑:

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.