选择和取消选择按钮

时间:2016-09-09 10:28:24

标签: android

我有一个文本视图列表,其中定义了一些目标。用户可以选择最多3个目标。他也可以取消选择他选择的目标并选择另一个。目前我所做的是用户只能选择3个目标,但如果用户取消选择任何目标并尝试选择另一个目标则不会发生。

这是我的代码:

public void onClick(View v) {


        switch (v.getId()) {
            case R.id.goalText1:
                if (count <= 2 && isSelected) {
                    goals.add(mGoal1.getText().toString());
                    mGoal1.setTextColor(getResources().getColor(R.color.black));
                    count++;
                    isSelected=false;
                    mGoal1.setSelected(true);

                } else {
                    isSelected = true;
                    goals.remove(mGoal1.getText().toString());
                    mGoal1.setTextColor(getResources().getColor(R.color.white));
                    mGoal1.setSelected(false);

                }
                break;

            case R.id.goalText2:

                if (count <= 2 && isSelected) {
                    goals.add(mGoal2.getText().toString());
                    mGoal2.setTextColor(getResources().getColor(R.color.black));
                    count++;
                    mGoal2.setSelected(true);
                    isSelected=false;
                } else {
                    isSelected = true;
                    goals.remove(mGoal1.getText().toString());
                    mGoal2.setTextColor(getResources().getColor(R.color.white));
                    mGoal2.setSelected(false);
                    Toast.makeText(getApplicationContext(), "select upto 3 fitness goals only " , Toast.LENGTH_LONG).show();

                }
                break;

            case R.id.goalText3:

                if (count <= 2 && isSelected) {
                    goals.add(mGoal3.getText().toString());
                    mGoal3.setTextColor(getResources().getColor(R.color.black));
                    count++;
                    mGoal3.setSelected(true);
                    isSelected=false;
                } else {
                    isSelected = true;
                    goals.remove(mGoal1.getText().toString());
                    mGoal3.setTextColor(getResources().getColor(R.color.white));
                    mGoal3.setSelected(false);
                    Toast.makeText(getApplicationContext(), "select upto 3 fitness goals only " , Toast.LENGTH_LONG).show();

                }
                break;

            case R.id.goalText4:

                if (count <= 2 && isSelected) {
                    goals.add(mGoal4.getText().toString());
                    mGoal4.setTextColor(getResources().getColor(R.color.black));
                    count++;
                    mGoal4.setSelected(true);
                    isSelected=false;
                } else {
                    isSelected = true;
                    goals.remove(mGoal1.getText().toString());
                    mGoal4.setTextColor(getResources().getColor(R.color.white));
                    mGoal4.setSelected(false);
                    Toast.makeText(getApplicationContext(), "select upto 3 fitness goals only " , Toast.LENGTH_LONG).show();

                }
                break;

1 个答案:

答案 0 :(得分:0)

首先将此逻辑应用于第一个目标,并检查它是否正常工作,然后您可以通过少量修改(如更改ID和所有内容)申请所有目标,

    case R.id.goalText1:
            if (goals.size <= 2 && ! mGoal1.isSelected()) 
            {
                goals.add(mGoal1.getText().toString());
                mGoal1.setTextColor(getResources().getColor(R.color.black));
                mGoal1.setSelected(true);

            } else {
                goals.remove(mGoal1.getText().toString());
                mGoal1.setTextColor(getResources().getColor(R.color.white));
                mGoal1.setSelected(false);

            }
            break;