重置微调器后,不要运行onItemSelected

时间:2014-06-28 02:57:41

标签: android spinner

我的应用程序中有3个微调器,我按

设置
    spinner_1.setOnItemSelectedListener(new OnItemSelectedListener()

        {

        public void onItemSelected(AdapterView<?> arg0, View arg1, 
        int arg2, long arg3)

        {
            run_function_A();   
        }

        });

spinner_3.setOnItemSelectedListener(new OnItemSelectedListener()

        {

        public void onItemSelected(AdapterView<?> arg0, View arg1, 
        int arg2, long arg3)

        {
            run_function_B();   
        }

        });

spinner_1.setOnItemSelectedListener(new OnItemSelectedListener()

        {

        public void onItemSelected(AdapterView<?> arg0, View arg1, 
        int arg2, long arg3)

        {
            run_functionC();    
        }

        });

上面的代码,看看Spinner Selected项目的运行函数,但是 我想用这样的按钮重置所有微调器

clear_all_spinner.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                spinner_1.setSelection(0);
                spinner_2.setSelection(0);
                spinner_3.setSelection(0);

            }
        });

我希望所有重置后都没有运行run_function_A(),run_function_B(),run_function_C()..怎么回事?

1 个答案:

答案 0 :(得分:0)

您可以尝试在所有微调器上设置setOnItemSelectedListener(null),然后将默认选定项重置为第0个索引,然后将setOnItemSelectedListener重新设置为您设置的项。我修改了你的代码如下:

clear_all_spinner.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            spinner_1.setOnItemSelectedListener(null);
            spinner_2.setOnItemSelectedListener(null);
            spinner_3.setOnItemSelectedListener(null);
            spinner_1.setSelection(0);
            spinner_2.setSelection(0);
            spinner_3.setSelection(0);
            spinner_1.setOnItemSelectedListener(new OnItemSelectedListener()
            {
                public void onItemSelected(AdapterView<?> arg0, View arg1, 
                        int arg2, long arg3)

                {
                    run_function_A();   
                }

            });

            spinner_3.setOnItemSelectedListener(new OnItemSelectedListener()

            {

                public void onItemSelected(AdapterView<?> arg0, View arg1, 
                        int arg2, long arg3)

                {
                    run_function_B();   
                }

            });

            spinner_1.setOnItemSelectedListener(new OnItemSelectedListener()

            {

                public void onItemSelected(AdapterView<?> arg0, View arg1, 
                        int arg2, long arg3)

                {
                    run_functionC();    
                }

            });
        }
    });

此外,我在您的代码中看到一个错误,您要为spinner_1设置setOnItemSelectedListener两次...请参阅run_functionA()和run_functionC()都设置为spinner_1选择。我已复制您的代码,但您需要对其进行修改。

最好在方法块中设置设置ItemSelectedListener的代码,这样就可以简单地调用方法,而不是在两个地方编写相同的代码。