在我的布局文件 - main.xml(线性布局)中,有2个按钮(比如b1和b2),1个listview和1个相对布局。当我点击b1时,列表视图为VISIBLE
,相对布局为GONE
。当我点击b2时,listview被设为GONE
并且相对布局为VISIBLE
。这是代码,
b1.setOnClickListener(new OnClickListener(){
public void onClick(View v)
{
try{ListView listView = (ListView) Home.this.findViewById(R.id.mylist);
listView.setTextFilterEnabled(true);
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2" ,"asdsad","asdsdsa","asdasdsa"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(context,
R.layout.list_item,values);
// Assign adapter to ListView
RelativeLayout rl=(RelativeLayout) findViewById(R.id.rl1);
rl.setVisibility(8);
listView.setAdapter(adapter);
listView.setVisibility(0);}
catch(Exception e){Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();}
}
});<br>
b2.setOnClickListener(new OnClickListener(){
public void onClick(View v)
{
try
{
ListView ls=(ListView) findViewById(R.id.mylist);
ls.setVisibility(8);
RelativeLayout rl=(RelativeLayout) findViewById(R.id.rl1);
getLayoutInflater().inflate(R.layout.creategroup,rl,true);
rl.setVisibility(0);
EditText username=(EditText) findViewById(R.id.group_owner_fill);
username.setText(userid);
username.setEnabled(false);
username.setFocusable(false);
boolean t=username.isEnabled();
String s=new Boolean(t).toString();
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
}
catch(Exception e)
{
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
}
}
});
我第一次点击b2时,Edittext(用户名)被锁定(即setenabled和setfocussable被设为false)。但是,当我单击b1然后单击b2时,Edittext未锁定,并且setText也不存在。可能的原因是什么?
答案 0 :(得分:3)
我建议您实施ViewFlipper,而不是设置可见性。因为ViewFlipper允许我们在用户需要时翻转视图。
<ViewFlipper
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/flip">
<ListView />
<RelativeLayout>
.............
.............
.............
</RelativeLayout>
</ViewFlipper>
在代码中,您可以使用setDisplayedChild()方法翻转视图。仅供参考,RelativeLayout是孩子1.所以你必须写下flipper.setDisplayedChild(1)