0

所有-我有两个并排的微调器。我希望这样,如果选择了 spinner1 中的项目,则 spinner2 是不可见的,反之亦然。换句话说,用户应该只能选择一个微调器。这是我关于微调器的代码:

 //Spinner 1
    List<String> spin1= new ArrayList() ;
    spin1.add("Select");
    spin1.add("1");
    spin1.add("2");
    spin1.add("3" );
    spin1.add("4");


    Spinner spinner1 = (Spinner) findViewById(R.id.houses);
    ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,
                R.layout.spinnerlayout, spin1);


    adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner1.setAdapter(adapter1);

    //Spinner 2
    List<String> spin2 = new ArrayList() ;
    spin2.add("Select");
    spin2.add("1");



    Spinner spinner2 = (Spinner) findViewById(R.id.hotels);
    ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this,
                R.layout.spinnerlayout, spin2);


    adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner2.setAdapter(adapter2);

欢迎使用示例代码。感谢您的时间。

4

1 回答 1

1

这个想法很简单:

    spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            if(position != 0)
                spinner2.setVisibility(View.GONE);
            else
                spinner2.setVisibility(View.VISIBLE);
        }

        public void onNothingSelected(AdapterView<?> parent) {}
    });

反之亦然spinner2。您需要更改 和 的定义,spinner1以便spinner2它们对整个班级可见:

public class ... {
    Spinner spinner1;
    Spinner spinner2;

    @Override
    public void onCreate() {
        spinner1 = ...
于 2012-07-06T17:11:13.327 回答