2016-08-04 22 views
0

レイアウトの1つは、ビューの表示のトグルを示します。レイアウトには、b1とb2の2つのボタンがあります。それぞれのボタンは、それぞれのRelativeLayoutを表示/拡大します。ボタンをクリックすると、RelativeLayoutの可視性が切り替わります。ビューの表示の切り替え

b1クリックで拡大されるRelativeLayoutには、edittextの実装があります。 ここで、b2はRelativeLayoutをTableLayoutで展開します。 これは私が表示するようにしようとしているものです: enter image description here

問題は時々TableLayoutはそうvisible.Likeされていない、次のとおりです。ここで enter image description here

はonClickの実装です:

public void onClick(View view) { 
    switch (view.getId()) { 
     case R.id.btnSearchByName: 
      if (rlSearchByName.getVisibility() == View.VISIBLE) { 
       rlSearchByName.setVisibility(View.GONE); 
      } else { 
       rlSearchByName.setVisibility(View.VISIBLE); 
      } 
      break; 
     case R.id.btnSearchByPartNo: 
      if (rlSearchByPartNo.getVisibility() == View.VISIBLE) { 
       rlSearchByPartNo.setVisibility(View.GONE); 
      } else { 
       rlSearchByPartNo.setVisibility(View.VISIBLE); 
      } 
      break; 

xmlファイル:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:paddingLeft="5dp" 
    android:paddingRight="5dp"> 

    <Button 
     android:id="@+id/btnSearchByName" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="7dp" 
     android:background="@color/transparent" 
     android:text="@string/search_by_name" 
     android:textColor="@color/orange" /> 

    <RelativeLayout 
     android:id="@+id/rlSearchByName" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#30828282" 
     android:paddingBottom="5dp" 
     android:paddingTop="3dp" 
     android:visibility="gone"> 

     <TextView 
      android:id="@+id/tvTitle" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/tv_search_by_name" 
      android:textColor="@color/grey" /> 

     <LinearLayout 
      android:id="@+id/llNameField" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/tvTitle" 
      android:orientation="horizontal"> 

      <EditText 
       android:id="@+id/etLastName" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:hint="Last Name" 
       android:textColorHint="#60828282" /> 

      <EditText 
       android:id="@+id/etFirstName" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:hint="First Name" 
       android:textColorHint="#60828282" /> 

      <EditText 
       android:id="@+id/etMiddleName" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:hint="Middle Name" 
       android:textColorHint="#60828282" /> 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/llNameField" 
      android:gravity="center" 
      android:orientation="horizontal"> 

      <Button 
       android:id="@+id/btnSearch" 
       android:layout_width="wrap_content" 
       android:layout_height="40dp" 
       android:background="@drawable/button_bg" 
       android:text="@string/search" 
       android:textColor="@color/white" /> 

      <Button 
       android:id="@+id/btnReset" 
       android:layout_width="wrap_content" 
       android:layout_height="40dp" 
       android:layout_marginLeft="10dp" 
       android:background="@drawable/button_bg" 
       android:text="@string/reset" 
       android:textColor="@color/white" /> 

     </LinearLayout> 


    </RelativeLayout> 

    <Button 
     android:id="@+id/btnSearchByPartNo" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="7dp" 
     android:background="@color/transparent" 
     android:text="@string/search_by_part_no" 
     android:textColor="@color/orange" /> 

    <RelativeLayout 
     android:id="@+id/rlSearchByPart" 
     android:layout_width="match_parent" 
     android:layout_height="80dp" 
     android:background="#30828282" 
     android:paddingBottom="5dp" 
     android:paddingTop="3dp" 
     android:visibility="gone"> 

     <TableLayout 
      android:id="@+id/tableLayout" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:visibility="visible"> 

      <TableRow 
       android:id="@+id/trPart12" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:clickable="true"> 

       <TextView 
        android:id="@+id/trPart1" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:layout_column="1" 
        android:layout_weight="1" 
        android:background="@drawable/border" 
        android:gravity="center" 
        android:text="1" 
        android:textSize="20sp" /> 

       <TextView 
        android:id="@+id/trPart2" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:layout_column="2" 
        android:layout_weight="1" 
        android:background="@drawable/border" 
        android:gravity="center" 
        android:text="2" 
        android:textSize="20sp" /> 
      </TableRow> 

      <TableRow 
       android:id="@+id/trPart34" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:clickable="true"> 

       <TextView 
        android:id="@+id/trPart3" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:layout_column="1" 
        android:layout_weight="1" 
        android:background="@drawable/border" 
        android:gravity="center" 
        android:text="3" 
        android:textSize="20sp" /> 

       <TextView 
        android:id="@+id/trPart4" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:layout_column="1" 
        android:layout_weight="1" 
        android:background="@drawable/border" 
        android:gravity="center" 
        android:text="4" 
        android:textSize="20sp" /> 
      </TableRow> 

     </TableLayout> 
    </RelativeLayout> 

    <LinearLayout 
     android:id="@+id/llResultView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:visibility="gone"> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="4dp" 
      android:layout_marginTop="10dp" 
      android:elevation="3dp" 
      android:text="@string/search_results" 
      android:textSize="14sp" /> 

     <android.support.v7.widget.RecyclerView 
      android:id="@+id/recyclerView" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_marginTop="5dp" 
      android:scrollbars="vertical" /> 
    </LinearLayout> 

</LinearLayout> 

答えて

0
make two variablr 
private boolean first; 
private boolean second; 

once you click on first buton 
then check condition like: 


if(first){ 
//visible 
first=false; 
}else{ 
//invisible 
first=true; 

} 
関連する問題