2

我的应用程序中有一个 Spinner,它的安装和配置如下:

  final Spinner left = getLeftShiftSpinner();  //simple gets the Spinner by Id
  left.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            left.setSelection(0);
            try {
                Log.d("test", "SHIFT_LEFT");
                String s = content.shiftLeft(i);
                content.clear();
                updateScreen(s);
            } catch (ScreenContent.WrongSyntaxException e) {
                Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {
        }
    });

当我编译所有内容并且应用程序在我的设备上启动时,我会从其中获得输出“SHIFT_LEFT”,Log.d(...)但我根本没有点击任何内容。应用程序只是启动,然后什么都不做就onItemSelectedListener被触发了??有谁知道为什么??

4

1 回答 1

2

当视图第一次膨胀/加载活动时,微调器选择其列表中的第一项,在onItemSelected侦听器上触发。

编辑:使用布尔变量忽略第一个触发器的示例

    boolean isInitTrigger = true;
    final Spinner left = getLeftShiftSpinner(); // simple gets the Spinner by Id
    left.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            if(isInitTrigger){
                isInitTrigger = false;
                return;
            }

            left.setSelection(0);
            try {
                Log.d("test", "SHIFT_LEFT");
                String s = content.shiftLeft(i);
                content.clear();
                updateScreen(s);
            } catch (ScreenContent.WrongSyntaxException e) {
                Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {
        }
    });
于 2013-10-01T19:33:09.977 回答