复选框 android 无效

Checkbox android not working

选中复选框时应使 Textview 可见。然而,这并没有发生。当我点击复选框时,它会被选中但 texview 不会出现。我已经在 XML 中将 textview 的可见性设置为 GONE。但是,当复选框被选中时,我将其设置为 VISIBLE。

View footer = inflater.inflate(R.layout.footer, viewGroup,
                    false);


   final CheckBox chb = (CheckBox) footer.findViewById(R.id.notify_confirm);
                final TextView notify_tv = (TextView) footer.findViewById(R.id.notify_tv);



                chb.setOnClickListener(new OnClickListener() {

              @Override
              public void onClick(View v) {

                if (chb.isChecked()) 
                {
                    //new CheckBoxThread().execute();
                      notify_tv.setVisibility(View.VISIBLE);    

                }
                else 
                {

                }

              }

            });

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
                              android:layout_width="fill_parent"
    android:layout_height="wrap_content"
               android:paddingLeft="6dp"
                android:orientation="vertical" 
                  android:background="#ffffff"
         >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="visible"

       >

         <CheckBox
             android:id="@+id/notify_confirm"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_marginLeft="10dp"
             android:text="Notify on Confirmation"
             android:textColor="#4d4d4d"
             android:paddingLeft="5dp"
             android:visibility="visible"

             />
         <TextView
             android:id = "@+id/notify_tv"
             android:layout_width="fill_parent"
             android:textColor = "@color/myPrimaryColor"
             android:gravity="center"
             android:visibility="gone"
             android:layout_height="wrap_content"
             android:text="You will be notified on confirmation"></TextView>

    </LinearLayout>
</LinearLayout>

我问这个问题可能有点天真,但我已经尝试了所有方法,但似乎没有用。

不使用 OnClickListener ,而是使用 OnCheckedChangeListener 作为复选框。按以下方式替换您的代码。

chb.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
    {
        if ( isChecked )
        {
            // perform logic
            notify_tv.setVisibility(View.VISIBLE);    
        }

    }
});
Add this property to the checkbox widget android:onClick="onCheckboxClicked"/>

Within the Activity that hosts this layout, the following method handles the click event for both checkboxes:

public void onCheckboxClicked(View view) {
    // Is the view now checked?
    boolean checked = ((CheckBox) view).isChecked();

    // Check which checkbox was clicked
    switch(view.getId()) {
        case R.id.notify_confirm:
            if (checked)
                //visible the textview here
            else
                //
            break;

根据你的onClickListener方式:

chkBox.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View v) {
                //is chkIos checked?
        if (((CheckBox) v).isChecked()) {
            Toast.makeText(MyAndroidAppActivity.this,
               "Bro, try Android :)", Toast.LENGTH_LONG).show();
        }
else{
//do else work
}

      }
    });

其他更好的方法是使用:

 CompoundButton.OnCheckedChangeListener:

如拉尔先生所述

谢谢

我检查了你的代码没问题,但只是方向(Layout Orientation)有点问题。 并且不要使用 TextView:visibility:"gone"(因为 gone 会更新您的视图并且您的视图会分散)而是使用 "invisible",它不会影响您的视图。

只需将 android:orientation="vertical" 放在第二个 LinearLayout 上即可。

这是您修改后的布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#ffffff"
    android:orientation="vertical"
    android:paddingLeft="6dp" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="visible" 
        android:orientation="vertical">

        <CheckBox
            android:id="@+id/notify_confirm"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:paddingLeft="5dp"
            android:text="Notify on Confirmation"
            android:textColor="#4d4d4d"
            android:visibility="visible" />

        <TextView
            android:id="@+id/notify_tv"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:gravity="center"
            android:text="You will be notified on confirmation"
            android:textColor="#000000"
            android:visibility="invisible" >
        </TextView>      
    </LinearLayout>
</LinearLayout>

这是您的 java 文件:

final CheckBox chb = (CheckBox) findViewById(R.id.notify_confirm);
final TextView notify_tv = (TextView)findViewById(R.id.notify_tv);
      chb.setOnClickListener(new OnClickListener() {

          @Override
          public void onClick(View v) {
            if (chb.isChecked()) 
            {
                //new CheckBoxThread().execute();
                  notify_tv.setVisibility(View.VISIBLE);    
            }
            else 
            {
                 notify_tv.setVisibility(View.INVISIBLE);   
            }
          }
        });

这会起作用...:)