문제

I am working on an android project with many activities and classes.

In few of the Layouts, I have a facility in which a user can select mark all so that all the checkboxes get selected.

But, the issue I am facing is I know only the one way (which is too lengthy) to do this - By creating a markall layout for each of those existing layouts, pasting the original layout code in the markall layout code and then giving all checkboxes as checked. This requires me to create xmls for all unmarkings also for each layout.

Please tell me a way in which I can create one xml for markall which has all checked checkboxes, and then call that layout on top of original layout when mark all is clicked.

도움이 되었습니까?

해결책

Lets assume you have following Layout

<LinearLayout
   android:id="@+id/checkbox_container"
   ...params>
   <CheckBox ...params/>
   <TextView ...params/>
   <CheckBox ...params/>
   <SomeView ...params/>
   <CheckBox ...params/>
</LinearLayout>

get your checkBoxLayout like this inside Activity

LinearLayout container = (LinearLayout)findViewById(R.id.checkbox_container);

Iterate over the childs inside the container like this:

for (int i = 0; i < container.getChildCount(); i++){
    View v = container.getChildAt(i);
    if (v instanceof CheckBox){
        CheckBox cb = (CheckBox)v;
        // isChecked should be a boolean indicating if every Checkbox should be checked or not
        cb.setChecked(isChecked)
    }
}

다른 팁

CheckBox MarkAllCheckBox =
    (CheckBox) findViewById( R.id.MarkAllCheckBox);
MarkAllCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
    {
        if ( isChecked )
        {
            checkBox1.setChecked("true");
            checkBox2.setChecked("true");
            checkBox3.setChecked("true");
            //so on
        }
    }
});
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top