jeudi 25 février 2016

android checkbox to string and verification

i have 12 check boxes in my layout. i need to make sure the user selects only 5, then press the save button. when he presses the save - the check boxes he selected should be shown in a text view and all other check boxes turn grey(unclickable). if he chooses more than 5 - toast tells him he must choose only 5. 1st problem i have is how to turn the other check boxes grey. 2nd problem is when he deselects 1 check box the size of my hashmap is still more than five.

here is code: public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {

Map<String, String> states = new HashMap<String, String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    CheckBox checkboxMovies = (CheckBox) findViewById(R.id.chkInterestsMovies);
    checkboxMovies.setOnCheckedChangeListener(this);
    CheckBox checkboxAnimals = (CheckBox) findViewById(R.id.chkInterestsAnimals);
    checkboxAnimals.setOnCheckedChangeListener(this);
    CheckBox checkboxShopping = (CheckBox) findViewById(R.id.chkInterestsShopping);
    checkboxShopping.setOnCheckedChangeListener(this);
    CheckBox checkboxBooks = (CheckBox) findViewById(R.id.chkInterestsBooks);
    checkboxBooks.setOnCheckedChangeListener(this);
    CheckBox checkboxRestaurants = (CheckBox) findViewById(R.id.chkInterestsRestaurants);
    checkboxRestaurants.setOnCheckedChangeListener(this);
    CheckBox checkboxComputers = (CheckBox) findViewById(R.id.chkInterestsComputers);
    checkboxComputers.setOnCheckedChangeListener(this);
    CheckBox checkboxTV = (CheckBox) findViewById(R.id.chkInterestsTV);
    checkboxTV.setOnCheckedChangeListener(this);
    CheckBox checkboxPubs = (CheckBox) findViewById(R.id.chkInterestsPubs);
    checkboxPubs.setOnCheckedChangeListener(this);
    CheckBox checkboxDancing = (CheckBox) findViewById(R.id.chkInterestsDancing);
    checkboxDancing.setOnCheckedChangeListener(this);
    CheckBox checkboxMusic = (CheckBox) findViewById(R.id.chkInterestsMusic);
    checkboxMusic.setOnCheckedChangeListener(this);
    CheckBox checkboxCoffe = (CheckBox) findViewById(R.id.chkInterestsCoffe);
    checkboxCoffe.setOnCheckedChangeListener(this);
    CheckBox checkboxOther = (CheckBox) findViewById(R.id.chkInterestsOther);
    checkboxOther.setOnCheckedChangeListener(this);

}


@Override
public void onCheckedChanged(final CompoundButton buttonView, boolean isChecked) {
    if (isChecked) {
        states.put(String.valueOf(buttonView.getId()), buttonView.getText().toString());
    } else {
        states.remove(buttonView.getId());
    }
    Button btnSave = (Button) findViewById(R.id.btnSave);
    btnSave.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (states.size() == 5) {
                List<String> selectedStrings = new ArrayList<String>(states.values());
                TextView chkEditText = (TextView) findViewById(R.id.chkEditText);
                chkEditText.setText(selectedStrings.toString());
            } else {
                Toast.makeText(MainActivity.this, "Only 5 are allowed", Toast.LENGTH_SHORT).show();
            }
        }
    });

}

}

can anyone plz help me?




Aucun commentaire:

Enregistrer un commentaire