设置微调器的可见性

时间:2012-07-06 17:06:33

标签: java android spinner visibility

全部 - 我有两个并排的旋转器。我希望它是如此,如果选择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);

示例代码欢迎。谢谢你的时间。

1 个答案:

答案 0 :(得分: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。您需要更改spinner1spinner2的定义,以便它们对整个班级可见:

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

    @Override
    public void onCreate() {
        spinner1 = ...