我的代码是:
public class Expense extends Activity{ Spinner datype; public voID onCreate(Bundle b){ super.onCreate(b); setContentVIEw(R.layout.expense); mainlayout=(linearLayout)this.findVIEwByID(R.ID.layout1); datype=(Spinner)findVIEwByID(R.ID.da_type); List<String>data1=new ArrayList<String>(); data1.add("Local"); data1.add("Ex-Station Double SIDe"); data1.add("Ex-Station Single SIDe"); data1.add("Out-Station Double SIDe"); data1.add("Out-Station Single SIDe"); ArrayAdapter<String>adapter=new ArrayAdapter<String>(this,androID.R.layout.simple_spinner_item,data1); adapter.setDropDownVIEwResource(androID.R.layout.simple_List_item_single_choice); datype.setAdapter(adapter); if(datype.getSelectedItem().toString().equals("Local")){ mainlayout.setVisibility(linearLayout.GONE); } else mainlayout.setVisibility(linearLayout.VISIBLE);}}解决方法 您需要设置OnItemSelectedListener并在那里更改您的布局.以下是您的活动实现OnItemSelectedListener的示例.
public class Expense extends Activity implements OnItemSelectedListener {private Spinner datype;private linearLayout mainlayout;public voID onCreate(Bundle b) { super.onCreate(b); setContentVIEw(R.layout.expense); mainlayout=(linearLayout)this.findVIEwByID(R.ID.layout1); datype=(Spinner)findVIEwByID(R.ID.da_type); List<String>data1=new ArrayList<String>(); data1.add("Local"); data1.add("Ex-Station Double SIDe"); data1.add("Ex-Station Single SIDe"); data1.add("Out-Station Double SIDe"); data1.add("Out-Station Single SIDe"); ArrayAdapter<String>adapter=new ArrayAdapter<String (this,data1); adapter.setDropDownVIEwResource(androID.R.layout.simple_List_item_single_choice); datype.setAdapter(adapter); if(datype.getSelectedItem().toString().equals("Local")){ mainlayout.setVisibility(linearLayout.GONE); } else { mainlayout.setVisibility(linearLayout.VISIBLE); } // here we set the Listener datatype.setonItemSelectedListener(this);}public voID onItemSelected(AdapterVIEw<?> parent,VIEw vIEw,int pos,long ID) { // here we change layout visibility again if(datype.getSelectedItem().toString().equals("Local")){ mainlayout.setVisibility(linearLayout.GONE); } else { mainlayout.setVisibility(linearLayout.VISIBLE); } }public voID onnothingSelected(AdapterVIEw<?> parent) {}}总结
以上是内存溢出为你收集整理的根据Android中的条件隐藏和显示布局全部内容,希望文章能够帮你解决根据Android中的条件隐藏和显示布局所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)