2

我有 8 个微调器:

        pl1sp = (Spinner) offersdialoglayout.findViewById(R.id.pl1offer);
        pl2sp = (Spinner) offersdialoglayout.findViewById(R.id.pl2offer);
        pl3sp = (Spinner) offersdialoglayout.findViewById(R.id.pl3offer);
        pl4sp = (Spinner) offersdialoglayout.findViewById(R.id.pl4offer);

        pl1scores = (Spinner) scoresdialoglayout.findViewById(R.id.pl1scores);
        pl2scores = (Spinner) scoresdialoglayout.findViewById(R.id.pl2scores);
        pl3scores = (Spinner) scoresdialoglayout.findViewById(R.id.pl3scores);
        pl4scores = (Spinner) scoresdialoglayout.findViewById(R.id.pl4scores);

我需要为他们设置 2 个听众。一个用于前 4 个微调器,一个用于最后 4 个。我不想重复这样的代码:

pl1sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            checkOffersSum(); // same method for first 4 spinners. for last 4 spinners is checkScoresSum()

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub.

        }

    });
    pl2sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

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

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub.

        }

    });

等等..所以我只需要写两次监听器,第一次是前 4 个微调器,第二个是最后 4 个微调器。谢谢

4

4 回答 4

2

为前 4 个微调器定义一个侦听器

Spinner[] offerspinners = {R.id.pl1offer, R.id.pl2offer, R.id.pl3offer, R.id.pl4offer}; 
for(Spinner offer : offerspinners){
    offersdialoglayout.findViewById(offer).
            setOnItemSelectedListener(offerListener);
}

AdapterView.OnItemSelectedListener offerListener = 
    new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            switch(parent.id){
                case R.id.pl1offer:
                    // offer 1 related code
                    break;
                case R.id.pl2offer:
                    // offer 2 related code
                    break;
            }
            // common code
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub.
        }
    }

而不是在这里用相同的代码定义第二个监听器

Spinner[] scorespinners = {R.id.pl1scores, R.id.pl2scores, R.id.pl3scores, R.id.pl4scores}; 
for(Spinner score : scorespinners){
    scoresdialoglayout.findViewById(score).
            setOnItemSelectedListener(scoreListener);
}

AdapterView.OnItemSelectedListener scoreListener = 
    new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            switch(parent.id){
                case R.id.pl1scores:
                    // score 1 related code
                    break;
                case R.id.pl2scores:
                    // score 2 related code
                    break;
            }
            // common code
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub.
        }
    }
于 2018-05-01T07:30:51.387 回答
1

您可以使用单个侦听器管理多个微调器。请检查这个答案。它处理同样的问题。希望能帮助到你。

于 2013-05-05T10:04:00.833 回答
1

按着这些次序:

  1. 在您的类中实现OnItemSelectedListener并覆盖未实现的方法onItemSelected(AdapterView<?> arg0, View view, int arg2, long arg3)

  2. 使用 pl1sp.setOnItemSelectedListener(this) 并为所有微调器执行此操作。

  3. 像这样使用覆盖方法onItemSelected(AdapterView<?> arg0, View view, int arg2, long arg3)

    @Override
    
    public void onItemSelected(AdapterView<?> arg0, View view, int arg2, long arg3) {
    
    
        int id = view.getId();
    
        if(id==R.id.pl1offer || id==R.id.pl2offer || id==R.id.pl3offer ||id==R.id.pl4offer){
    
            // do this
        }
    
        else{
            //do this
        }
    }
    
于 2013-05-05T10:30:28.147 回答
0

您可以做的是创建一个包含所有微调器的数组,然后使用 for 循环将每个单独的微调器附加到 setItemSelected() 方法,即

Spinner[] spinner = {pl1sp, pl2sp, pl3sp, pl4sp}; 
for(Spinner individualSpinner : spinner){
individualSpinner.etOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

    public void onItemSelected(AdapterView<?> arg0, View arg1,
            int arg2, long arg3) {
           //Custom method here

    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub.

    }

});

Spinner[] spinners = {pl1scores, pl2scores, pl3scores, pl4scores}; 
for(Spinner individualSpinner2 : spinner){
individualSpinner.etOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

    public void onItemSelected(AdapterView<?> arg0, View arg1,
            int arg2, long arg3) {
           //Custom method here

    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub.

    }

});
于 2018-02-06T09:06:52.110 回答