5

我是安卓新手。我想根据 if else 条件隐藏和显示 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);


}

}
4

5 回答 5

9

您还需要在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) {
}
}
于 2013-08-31T06:45:35.540 回答
8

你可以用这个

反而

mainlayout.setVisibility(LinearLayout.GONE);

mainlayout.setVisibility(View.GONE);

在 Spinner 的单击事件之后,您必须隐藏并显示您的代码,只需此 OnItemSelectedListener方法。

于 2013-08-31T06:40:15.047 回答
2

而不是线性 layout.gone 尝试 View.GONE

于 2013-08-31T06:37:08.767 回答
2

并且还添加

机器人:可见性=“可见”

在您的主布局xml中。 然后用这个

mainlayout.setVisibility(View.GONE);

在你的代码中

于 2015-11-12T06:10:55.480 回答
0

您可以隐藏整个视图,而不是隐藏布局,如下所示

  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(View.GONE);
    }
    else {
        mainlayout.setVisibility(View.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) {
}
}
于 2019-06-02T02:37:04.110 回答