简体   繁体   中英

ANDROID: clicking multiple checkboxes

I want to show the name of those multiple checkboxes that was checked. The problem is that when I check on any of them, only the last one checked is written on the textview. How can I resolve this?

I don't really understand how I will concatenate the names of those checkboxes.

    public void ChkCommand(View v)
{
    txtans = (EditText)findViewById(R.id.txtcon1);

    if(v.getId() == R.id.chk1 && ((CheckBox)v).isChecked()){
        chkClick = (CheckBox) findViewById(R.id.chk1);
    }
    else if(v.getId() == R.id.chk2 && ((CheckBox)v).isChecked()){
        chkClick = (CheckBox) findViewById(R.id.chk2);
    }
    else if(v.getId() == R.id.chk3 && ((CheckBox)v).isChecked()){
        chkClick = (CheckBox) findViewById(R.id.chk3);
    }

    String value = chkClick.getText().toString();
    txtans.setText(value);

}

public void CloseConF(View v)
{
    Intent intent = new Intent(this, SampComponents.class);
    startActivity(intent);
    finish();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.con_chk_samp, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.activity_con_chk_samp,
                container, false);
        return rootView;
    }
}

}

Each time you want to display the state of all your three checkboxes, you have to check all three:

String value = "";
if (((CheckBox) findViewById(R.id.chk1)).isChecked()) {
    value += (CheckBox) findViewById(R.id.chk1).getText().toString();
}
if (((CheckBox) findViewById(R.id.chk2)).isChecked()) {
    value += (CheckBox) findViewById(R.id.chk2).getText().toString();
}
if (((CheckBox) findViewById(R.id.chk3)).isChecked()) {
    value += (CheckBox) findViewById(R.id.chk3).getText().toString();
}

txtans = (EditText)findViewById(R.id.txtcon1);
txtans.setText(value);

Then of course there would be some refactoring to do to get something cleaner, but that should get you started.

Make value a field

private String value = "";

then change

String value = chkClick.getText().toString();

to something as:

value = value + " / " + chkClick.getText().toString();

But it's also necessary to tweak a bit the logic for reading the status from the checkboxes, otherwise they will add text also while un-checking.

Try getting the checkbox text in every if else and append to a string value like this. chkBox1, chkBox2 and chkBox3 will be CheckBoxes in your java class.

public void ChkCommand(View v)
{
    String value="";
    txtans = (EditText)findViewById(R.id.txtcon1);

    if(chkBox1.isChecked())
    {
        /*((CheckBox) findViewById(R.id.chk3)).setChecked(false);
        ((CheckBox) findViewById(R.id.chk2)).setChecked(false);*/
        chkClick = (CheckBox) findViewById(R.id.chk1);
        value += chkClick.getText().toString()+", ";

    }
    if(chkBox2.isChecked())
    {
        /*((CheckBox) findViewById(R.id.chk1)).setChecked(false);
        ((CheckBox) findViewById(R.id.chk3)).setChecked(false);*/
        chkClick = (CheckBox) findViewById(R.id.chk2);
        value += chkClick.getText().toString()+", ";
    }
    if(chkBox3.isChecked())
    {/*
        ((CheckBox) findViewById(R.id.chk1)).setChecked(false);
        ((CheckBox) findViewById(R.id.chk2)).setChecked(false);*/
        chkClick = (CheckBox) findViewById(R.id.chk3);
        value += chkClick.getText().toString();
    }

    txtans.setText(value);
}

So you will get the result with all checked checkboxes' text. Hope this helps.

PS: I just realized from @njzk2 's comment that you will need to changed else if to if in order to get all checkboxes's state.

Declare StringBuffer or StringBuilder member variables

StringBuffer buff = new StringBuffer();

Try this

 buff.append(chkClick.getText().toString()+" ");  
            txtans.setText(buff.toString());

in place of

String value = chkClick.getText().toString();
           txtans.setText(value);

It should work if your checkbox checking action is working correctly

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