4

我正在创建一个包含两个微调器的 android 活动。我了解如何使用回调函数实现onItemSelectedListenerfor one spinner :onItemSelected

    public void onItemSelected(AdapterView<?> parent, View view,
                           int pos, long id) {
    Spinner spinner = (Spinner) findViewById(R.id.spinnerOneOfTwo);
    spinner.setOnItemSelectedListener(this);
    //do things with selection...
}

但是,如果我有多个微调器怎么办?在我看来,我需要一个单独的函数,但由于两个微调器都设置为回调,onItemSelected()我不能采用这种方法。有什么方法可以判断哪个微调器正在调用该onItemSelected()函数?也许微调器正在调用的参数键之一?然后我可以将它的 ID 设置为我的微调器变量 ID 的参数吗?

我知道有一种方法(我绝对不是唯一一个将多个微调器放在一个活动中的人),非常感谢任何提示!

4

6 回答 6

19

让您的 2 个 Spinner 实现相同的功能OnItemSelectedListener并尝试以下操作:

public void onItemSelected(AdapterView<?> parent, View view,
            int pos, long id) {
        switch (parent.getId()) {
        case R.id.your_spinner_1_id:
            // do stuffs with you spinner 1
            break;
        case R.id.your_spinner_2_id:
            // do stuffs with you spinner 2
            break;
        default:
            break;
        }
    }

希望这可以帮助。

于 2013-09-10T04:23:26.300 回答
2

您可以为所有 Spinners 设置相同的 onItemSelected 侦听器 .. 像

spinner1.setOnItemSelectedListener(this);
spinner2.setOnItemSelectedListener(this);

并使用微调器的 id 以单一方法实现动作

@Override
    public void onItemSelected(AdapterView<?> spinner, View view,
                                                  int position,long arg3) 
    {
    int id = spinner.getId();  //You can also use int id= view.getId();
    switch (id) 
    {
            case R.id.spinner1:
            // Do what you want
            break;
        case R.id.spinner2:
           // Your another task
        break;

    }
于 2013-09-10T04:34:11.823 回答
0

函数调用包括哪个适配器视图负责进行调用。

因此,只需检查哪个适配器视图调用了您的函数并让代码处理相应的适配器。

于 2013-09-10T04:24:06.350 回答
0

使用此示例使用多个onItemSelectedListeners

OnItemSelectedListener myListener=new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
             switch (arg2) {
                case 1:
                    Toast.makeText(SpinnerActivity.this,"Spinner 1", Toast.LENGTH_LONG).show();
                    break;
                case 2:
                    Toast.makeText(SpinnerActivity.this, "Spinner 2", Toast.LENGTH_LONG).show();         
                    break;
                case 3:
                    Toast.makeText(SpinnerActivity.this, "Spinner 2", Toast.LENGTH_LONG).show();
                    break;
                case 4:
                    Toast.makeText(SpinnerActivity.this,"Spinner 2", Toast.LENGTH_LONG).show();
                    break;
                case 5:
                    Toast.makeText(SpinnerActivity.this, "Spinner 2", Toast.LENGTH_LONG).show();
                    break;
                }

        }

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

        }
    };
于 2013-09-10T04:25:37.143 回答
0

使用匿名接口提高可读性

spin1.setOnItemSelectedListener(new OnItemSelectedListener() {

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

            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {

            }
        });

        spin2.setOnItemSelectedListener(new OnItemSelectedListener() {

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

            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {

            }
        });
于 2013-09-10T04:27:00.817 回答
0

这样做,

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Spinner;
import android.widget.Toast;

import com.meta.R;

public class SpinnerActivity extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Spinner sp1, sp2, sp3, sp4, sp5;
    sp1 = (Spinner) findViewById(R.id.spinner1);
    sp2 = (Spinner) findViewById(R.id.spinner2);
    sp3 = (Spinner) findViewById(R.id.spinner3);
    sp4 = (Spinner) findViewById(R.id.spinner4);
    sp5 = (Spinner) findViewById(R.id.spinner5);

    sp1.setOnItemSelectedListener(myListener);
    sp2.setOnItemSelectedListener(myListener);
    sp3.setOnItemSelectedListener(myListener);
    sp4.setOnItemSelectedListener(myListener);
    sp5.setOnItemSelectedListener(myListener);



}

OnItemSelectedListener myListener=new OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
         switch (arg2) {
            case 1:
                Toast.makeText(SpinnerActivity.this,"Spinner 1", Toast.LENGTH_LONG).show();
                break;
            case 2:
                Toast.makeText(SpinnerActivity.this, "Spinner 2", Toast.LENGTH_LONG).show();         
                break;
            case 3:
                Toast.makeText(SpinnerActivity.this, "Spinner 2", Toast.LENGTH_LONG).show();
                break;
            case 4:
                Toast.makeText(SpinnerActivity.this,"Spinner 2", Toast.LENGTH_LONG).show();
                break;
            case 5:
                Toast.makeText(SpinnerActivity.this, "Spinner 2", Toast.LENGTH_LONG).show();
                break;
            }

    }

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

    }
};



}
于 2013-09-10T04:29:09.307 回答