2015-06-17 2 views
0

Я пытаюсь просто скрыть линейный макет, если щелкнуть по нему. Я определил функцию, и атрибут onClick вызывает его, который также проверяется с помощью тоста, но когда я пытаюсь скрыть макет, он ничего не делает.hide layout checkbox is checked android

layout.xml:

<CheckBox 
     android:id="@+id/cbguest" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="0.9" 
     android:onClick="itemClicked" 
     /> 

В MainActivity.java

int[] i = {0}; 
public void itemClicked(View v) { 
    LinearLayout layoutguest1 = (LinearLayout) findViewById(R.id.guestlayout); 

    if (lns[0]%2 == 0) { 
     Toast.makeText(getApplicationContext(),"Visible", Toast.LENGTH_SHORT).show(); 
     layoutguest1.setVisibility(View.VISIBLE); 
     lns[0]+=1; 
    } 
    if (lns[0]%2 != 0) { 
     layoutguest1.setVisibility(View.GONE); 
     Toast.makeText(getApplicationContext(),"Gone", Toast.LENGTH_SHORT).show(); 
     lns[0]+=1; 
    } 

} 

ответ

0

Вы должны использовать onCheckedChangeListener как я не думаю, что он регистрирует как onClick - но onCheck.

Android: checkbox listener

1
**activity_main.xml 
put this code in activity_main.xml file** 
<RelativeLayout 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" 
tools:context="com.example.demo.MainActivity" > 

<LinearLayout 
    android:id="@+id/layoutLogin" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" > 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="UserName" /> 

     <EditText 
      android:layout_width="200dp" 
      android:layout_height="wrap_content" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" > 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Password" /> 

     <EditText 
      android:layout_width="200dp" 
      android:layout_height="wrap_content" /> 
    </LinearLayout> 
</LinearLayout> 

<CheckBox 
    android:id="@+id/chkBox" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" 
    android:text="Hide" /> 

</RelativeLayout> 


**MainActivity.java 
put this code in MainActivity.java file** 

public class MainActivity extends Activity { 

private LinearLayout layoutLogin; 
private CheckBox chkBox; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    chkBox = (CheckBox) findViewById(R.id.chkBox); 
    layoutLogin = (LinearLayout) findViewById(R.id.layoutLogin); 

    chkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

     @Override 
     public void onCheckedChanged(CompoundButton buttonView, 
boolean   isChecked) 
     { 
      // TODO Auto-generated method stub 
      if(isChecked) 
      { 
       layoutLogin.setVisibility(View.GONE); 
       chkBox.setText("UnHide"); 
      } 
      else 
      { 
       layoutLogin.setVisibility(View.VISIBLE); 
       chkBox.setText("Hide"); 
      } 
     } 
    }); 
} 

}