Android 如何在下次单击时更改按钮的背景

Android 如何在下次单击时更改按钮的背景,android,Android,目前,我有一个TextViews列表,第一次单击时背景会发生变化。但我也希望当用户第二次点击时,它的背景应该再次改变,反之亦然 使用我现有的代码,我只能在第一次单击时更改背景,当我再次单击时,背景不会更改 这是我的密码: public void onClick(View v) { switch (v.getId()) { case R.id.goalText1: if (count <= 2) {

目前,我有一个
TextView
s列表,第一次单击时背景会发生变化。但我也希望当用户第二次点击时,它的背景应该再次改变,反之亦然

使用我现有的代码,我只能在第一次单击时更改背景,当我再次单击时,背景不会更改

这是我的密码:

 public void onClick(View v) {


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

                } else {
                    Toast.makeText(SelectGoal.this, "cant select more", Toast.LENGTH_SHORT).show();
                }
                break;

            case R.id.goalText2:

                if (count <= 2) {
                    goals.add(mGoal2.getText().toString());
                    mGoal2.setTextColor(getResources().getColor(R.color.black));
                    mGoal2.setBackgroundColor(getResources().getColor(R.color.white));
                    count++;
                } else {
                    Toast.makeText(SelectGoal.this, "cant select more", Toast.LENGTH_SHORT).show();
                }
                break;

            case R.id.goalText3:

                if (count <= 2) {
                    goals.add(mGoal3.getText().toString());
                    mGoal3.setTextColor(getResources().getColor(R.color.black));
                    mGoal3.setBackgroundColor(getResources().getColor(R.color.white));
                    count++;
                } else {
                    Toast.makeText(SelectGoal.this, "cant select more", Toast.LENGTH_SHORT).show();
                }
                break;

            case R.id.goalText4:

                if (count <= 2) {
                    goals.add(mGoal4.getText().toString());
                    mGoal4.setTextColor(getResources().getColor(R.color.black));
                    mGoal4.setBackgroundColor(getResources().getColor(R.color.white));
                    count++;
                } else {
                    Toast.makeText(SelectGoal.this, "cant select more", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.goalText5:

                if (count <= 2) {
                    goals.add(mGoal5.getText().toString());
                    mGoal5.setTextColor(getResources().getColor(R.color.black));
                    mGoal5.setBackgroundColor(getResources().getColor(R.color.white));
                    count++;
                } else {
                    Toast.makeText(SelectGoal.this, "cant select more", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.goalText6:

                if (count <= 2) {
                    goals.add(mGoal6.getText().toString());
                    mGoal6.setTextColor(getResources().getColor(R.color.black));
                    mGoal6.setBackgroundColor(getResources().getColor(R.color.white));
                    count++;
                } else {
                    Toast.makeText(SelectGoal.this, "cant select more", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.goalText7:

                if (count <= 2) {
                    goals.add(mGoal7.getText().toString());
                    mGoal7.setTextColor(getResources().getColor(R.color.black));
                    mGoal7.setBackgroundColor(getResources().getColor(R.color.white));
                    count++;
                } else {
                    Toast.makeText(SelectGoal.this, "cant select more", Toast.LENGTH_SHORT).show();
                }
                break;


            case R.id.btnGoal:
                Intent intent = new Intent(this, fiteness_level_selection.class);


                try {
                    obj.put("SelectedGoal", goals);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                intent.putExtra("GoalJson", obj.toString());
                startActivity(intent);
                break;


        }
public void onClick(视图v){
开关(v.getId()){
案例R.id.goalText1:

如果(count据我所知,您希望更改背景,您已使用
bool
变量,并在
按钮中单击
以检查
bool

Boolean click_firsttime = false;
按钮内
单击

这样

If(click_firsttime == false){
//perform task 

// and set bool value to true
click_firsttime  = true;
}else{

// perform task and set bool value

click_firsttime  = false;
}

首先创建一个布尔值:

 private boolean foo = true;
然后按如下方式更改代码:

public void onClick(View v) {


    switch (v.getId()) {
        case R.id.goalText1:
            if (foo) {
                goals.add(mGoal1.getText().toString());
                mGoal1.setTextColor(getResources().getColor(R.color.black));
                mGoal1.setBackgroundColor(getResources().getColor(R.color.white));
                count++;
                foo = false;
            } else {
                // set background to another color
                foo = true;
            }
            break;

在活动中设置布尔数组

//suppose you've 3 textviews within listview     
public boolean isTransparent[] = {false,false,false};
然后在onClick方法中执行此操作

public void onOnclick(View v){
    switch (v.getId()) {
        //foreach textview do this
        case R.id.textView1 : 
            int color;
            if (isTransparent[0])
                color = getResources().getColor(android.R.color.transparent);
            else
                color = getResources().getColor(R.color.white);
            isTransparent[0]=!isTransparent[0];
            textview1.setBackgroundColor(color);
            break;
       case R.id.textView2:
            // now check for isTransparent[1]
            and so on ...
            ...

    }
}

单击textview,是否要在提供的2种或更多颜色之间切换?因此,您希望textview交替更改其颜色?(在哪些颜色之间?@Protino白色和透明颜色之间?)