android-setOnClickListeners用于动态创建的复选框

尝试在我的应用程序中创建动态CheckBoxes时遇到问题.设计工作正常,我可以创建任意数量的复选框.复选框与TextView一起放入TableRow中,以便文本位于复选框的左侧.但是我的问题是,在我的活动中,无论是否选中,我都可以获取复选框的“状态”.
我使用充气机创建我的复选框.复选框的xml:

<TableRow xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/TableRow" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:paddingLeft="20dip" android:id="@+id/tv_effect" android:gravity="left" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:textColor="#000" android:textSize="18dip" android:layout_width="wrap_content"></TextView>
<CheckBox android:id="@+id/cb_effect" android:layout_height="wrap_content" android:text="" android:layout_gravity="right" android:layout_width="wrap_content"></CheckBox>

调用函数来创建一个包含textview和复选框的新tablerow:

    public void layoutMakeSpeakerEffect(String effectName,int effectNumber)
{
    LayoutInflater linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View myViewSp = linflater.inflate(R.layout.speaker_settings, null);

    final TextView tv_effect = (TextView) myViewSp.findViewById(R.id.tv_effect);
    tv_effect.setId(effectNumber);
    tv_effect.setText(effectName);

    final CheckBox cb_effect = (CheckBox) myViewSp.findViewById(R.id.cb_effect);
    cb_effect.setId(effectNumber);
    cb_effect.setText("");

    cb_effect.setonClickListener(new OnClickListener() {
          @Override
          public void onClick(View v) { 
              if(cb_effect.isChecked())
              {
                  Toast toast = Toast.makeText(getApplicationContext(), ""+tv_effect.getText()+": ON", Toast.LENGTH_SHORT);
                  toast.show();
              }
              else
              {
                  Toast toast = Toast.makeText(getApplicationContext(), ""+tv_effect.getText()+": OFF", Toast.LENGTH_SHORT);
                  toast.show(); 
              }
          }
    });

    tl_speakerSettings.addView(myViewSp);   
}

如前所述,设计工作正常.
但是我如何能够在此功能之外使复选框中的“状态”消失呢?
我还需要一个可以清除复选框状态的功能,以及另一个可以启用和禁用复选框的功能

我似乎无法自己解决这个问题.

我唯一的想法是做一些检查“ cb_effects” ID的事,然后检查所需复选框的状态.

解决方法:

请改用setOnCheckedChangeListener.它将触发onCheckedChanged(CompoundButton buttonView,boolean isChecked)方法,您现在可以在其中读取isChecked变量的复选框状态.

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...