2017-04-06 62 views
0

我有具有被动态创建在TableRowCheckBox一个Buttonadd_Connection 启用按钮。机器人:如果复选框中的TableRow被选中

我想启用如有CheckBox被检查add_Connection, 当我使用此代码Button启用禁用工作正常,但如果我检查CB1和CB2按钮启用 如果我未选中CB2和CB1仍处于选中它点击完成后禁用Button

这里

cb: check box

add_connection: Button

cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() 
        { 
         @Override 
         public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
         { 
           add_Connections.setEnabled(isChecked); 
         } 
        }); 
+0

我想你没有正确管理状态。您需要跟踪所有复选框,如果其中任何一个选中或不选中。因为您取消选择cb2,上下文仅在该复选框的周围,因此isChecked == false。你需要一个复选框列表,看看它们是否被选中。按钮应该做这个检查,遍历列表,每当它发现一个复选框被选中时,启用按钮并返回。 –

+0

我创造了动态 如果使用3 CB的复选框,CB1,CB2,CB3所有被点击和他们中的任何两个未被选中,一个被选中它仍然禁用该按钮 –

+0

以及肯定的,因为你做的最后一个动作是你取消选择复选框并自动触发回调add_connection.setEnabled(false)。它不知道其他复选框的状态。刚刚意识到,您需要在复选框回调中不选中按钮,因为您从复选框控制按钮 –

回答

0
import android.os.Bundle; 
    import android.support.v7.app.AppCompatActivity; 
    import android.util.Log; 
    import android.widget.Button; 
    import android.widget.CheckBox; 
    import android.widget.CompoundButton; 

    import java.util.ArrayList; 

    public class MainActivity extends AppCompatActivity { 

     CheckBox cb1; 
     CheckBox cb2; 
     CheckBox cb3; 
     Button button; 

     ArrayList<CheckBox> checkboxesList = new ArrayList<>(); 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 
      cb1 = (CheckBox) findViewById(R.id.cb1); 
      cb2 = (CheckBox) findViewById(R.id.cb2); 
      cb3 = (CheckBox) findViewById(R.id.cb3); 
      button = (Button) findViewById(R.id.button); 

      button.setEnabled(false); 

      cb1.setTag("cb1"); 
      cb2.setTag("cb2"); 
      cb3.setTag("cb3"); 

      checkboxesList.add(cb1); 
      checkboxesList.add(cb2); 
      checkboxesList.add(cb3); 

      CheckBoxListener listener = new CheckBoxListener(checkboxesList, button); 

      for (CheckBox checkbox : checkboxesList) { 
       checkbox.setOnCheckedChangeListener(listener); 
      } 
     } 

     public static class CheckBoxListener implements CompoundButton.OnCheckedChangeListener { 

      ArrayList<CheckBox> list; 
      Button button; 

      public CheckBoxListener(ArrayList<CheckBox> checkboxesList, Button btn) { 
       list = checkboxesList; 
       button = btn; 
      } 

      private boolean checkState() { 
       for (CheckBox checkbox : list) { 
        if (checkbox.isChecked()) { 
         return true; 
        } 
       } 
       return false; 
      } 

      @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
       button.setEnabled(checkState()); 
       Log.d("TAG", "Button is: " + checkState()); 
      } 
     } 
    } 


and layout 

<?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:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:orientation="vertical" 
    tools:context="com.thomsonreuters.alexandrugoja.myapplication.MainActivity"> 

    <CheckBox 
     android:id="@+id/cb1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <CheckBox 
     android:id="@+id/cb2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <CheckBox 
     android:id="@+id/cb3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <Button 
     android:id="@+id/button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

</LinearLayout> 
+0

这只是一个粗略的例子。我认为,除非你有充分的理由不使用TableLayout,否则可以使用ListView或更好的RecyclerView。我想让你看到的是我用checkState()方法跟踪所有复选框。如果选中任何一个复选框,该方法返回true,否则返回false。你在做什么是你只检查一个复选框,这将永远是你按下的最后一个复选框,这就是为什么你确实有这种行为。 –

+0

我想使用tablelayout bc我想要列标题,并且所有行应该动态添加 所以是有任何方式来跟踪每一行的复选框 和一件事情如何共享屏幕快照在这里在stackoverflow(我我在这里新) –

+0

哎对其做:) 感谢刚刚登录在今天帮助 –