简体   繁体   中英

Choose checkbox checked programmatically android

I'm working on android app that creat layout with textView & checkbox programmatically for each text user input in EditText, and when the user select one of the checkbox and click on delete button the layout that contain that checkbox remove from the main layout

public void plusBtn(View view)
{
    item = actv.getText().toString(); // text from EditText
    actv.setText("");
    creatt();
    
}

public void deletBtn(View view)
{
    if(chbox.isChecked()){
    
        linear.removeView(linr);
        }   
}
public void creatt()
{
    linr = new LinearLayout(this);
    linr.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    linr.setOrientation(LinearLayout.HORIZONTAL);
    linr.setGravity(Gravity.RIGHT);
    TextView txt = new TextView(this);
    txt.setText(item);
    linr.addView(txt);
    chbox = new CheckBox(this);
    linr.addView(chbox);
    linear.addView(linr); // main layout
}

But when I click on delete button just the last layout removed, and that not what I want.

though the technique you're using is not fine. but let me give you a shortcut. in your deletBtn method try to get the reference of the desired deleted view (try to handle case of id or boolean you've to make logic) and enclose this line linear.removeView(linr); into that condition.

Summery, only delete the view which meets the condition.

Alternative, do your whole task from scratch with the help of Recycler-view. as it Recycler-view will give you exact location of cell.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM