我是Android新手。我想根据if条件隐藏和显示Linearlayout。在我的申请中,我采取了1个旋转器。根据选定的微调器值,我希望我的下一个布局隐藏或可见,但如果它一旦消失,它就不再可见了。
我的代码是:
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);
}
}
答案 0 :(得分:7)
您需要设置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,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);
}
// 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) {
}
}
答案 1 :(得分:5)
你可以用这个
代替
mainlayout.setVisibility(LinearLayout.GONE);
的
mainlayout.setVisibility(View.GONE);
您必须在Spinner点击事件后隐藏并显示您的代码 OnItemSelectedListener 方法..
答案 2 :(得分:0)
而不是线性布局。尝试使用View.GONE
答案 3 :(得分:0)
并添加
mainlayout xml 中的<强>机器人:能见度=&#34;可见&#34; 强>
。 然后使用此
代码中的<强> mainlayout.setVisibility(View.GONE); 强>
答案 4 :(得分:0)
除了隐藏布局之外,您还可以隐藏整个视图,如下所示:
def reverse_chunks(s, k):
L = [s[i:i+k] for i in range(0, len(s), k)] # this returns ['Meh', 'ran', 'Sah', 'ami']
result = ""
for e in L:
result += e[::-1] # [::-1] reverses any string
return result
print (reverse_chunks("MehranSahami", 3)) # this returns "heMnarhaSima"