0

我有 3 个旋转器。我正在使用计时器自动显示微调器的内容。

当我运行应用程序时。微调器的内容同时运行。

Now i would like to make the spinners's running when spinner1 is chosen then spinner2 is activated(running), when spinner2 is chosen then spinner3 is activated(running)

这是我的代码...

public void addListenerOnButton() {

        spinner1 = (Spinner) findViewById(R.id.spinner1);
        spinner2 = (Spinner) findViewById(R.id.spinner2);
        spinner3 = (Spinner) findViewById(R.id.spinner3);

        btn_choose1 = (Button) findViewById(R.id.button1);
        btn_choose2 = (Button) findViewById(R.id.button2);
        btn_choose3 = (Button) findViewById(R.id.button3);

        timer1 = new Timer();
        timer1.schedule(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if(spinner1.getSelectedItemPosition() == (Arrays.asList(getResources().getStringArray(R.array.cincin_warna)).size()-1)){
                            spinner1.setSelection(0);
                        }else{
                            spinner1.setSelection(spinner1.getSelectedItemPosition()+1);
                        }
                    }
                });
            }
        },0,1000);

        btn_choose1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(timer1!=null){
                    timer1.cancel();
                }
            }
        });

        timer2 = new Timer();
        timer2.schedule(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if(spinner2.getSelectedItemPosition() == (Arrays.asList(getResources().getStringArray(R.array.cincin_warna)).size()-1)){
                            spinner2.setSelection(0);
                        }else{
                            spinner2.setSelection(spinner2.getSelectedItemPosition()+1);
                        }
                    }
                });
            }
        },0,1000);

        btn_choose2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(timer2!=null){
                    timer2.cancel();
                }
            }
        });

        timer3 = new Timer();
        timer3.schedule(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if(spinner3.getSelectedItemPosition() == (Arrays.asList(getResources().getStringArray(R.array.cincin_warna)).size()-1)){
                            spinner3.setSelection(0);
                        }else{
                            spinner3.setSelection(spinner3.getSelectedItemPosition()+1);
                        }
                    }
                });
            }
        },0,1000);

        btn_choose3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(timer3!=null){
                    timer3.cancel();
                }
            }
        });
4

0 回答 0