2017-04-06 68 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

哎對其做:) 感謝剛剛登錄在今天幫助 –