如何首先更改按钮的颜色单击并在列表视图中第二次单击时恢复以前的颜色?

时间:2014-12-15 08:27:44

标签: android listview onclick

这是我正在做的代码片段。它不起作用。我究竟做错了什么?

我正在使用listview。 我使用过Viewholder。 我在XML中初步分配了color_green。 我希望每次点击时颜色变为红色,再次点击时它会变回绿色。

这是我的getView

@Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder holder;

        if (convertView == null) {
            convertView = getLayoutInflater().inflate(R.layout.rowlist, null);
            holder = new ViewHolder();
            holder.name = (TextView) convertView.findViewById(R.id.textView1);
            holder.button1 = (Button) convertView.findViewById(R.id.button1);
            holder.button2 = (Button) convertView.findViewById(R.id.button2);
            holder.img = (ImageView)convertView.findViewById(R.id.imageView1);
            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }

        Student student = getItem(position);

        holder.name.setText(student.getName());
        holder.button2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //  int x = (Integer) holder.button1.getTag(position);
                Toast.makeText(MainActivity.this, "Clicked on "+position+"", Toast.LENGTH_SHORT).show();
            }
        });
        holder.button1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

            }
        });
        return convertView;
    }

我希望在button1的onClickListener中实现以下功能

        final Button button1 = (Button) itemView.getTag(R.id.button1);
        final int currentPostion1 = position;
        button1.setOnClickListener(new OnClickListener() {
            ColorDrawable buttonColor = (ColorDrawable) button1.getBackground();
            int colorId = buttonColor.getColor();
            @Override
            public void onClick(View v) {


                Log.d("msg", "Inside onClick");
                if(colorId==R.color.color_green)
                {
                    Log.d("msg", "Inside onClick1 if"); 
                    button1.setBackgroundColor(R.color.color_red);
                    //Toast.makeText(MainActivity.this, " "+currentPostion1+" ", Toast.LENGTH_SHORT).show();
                    //flag=0;
                }
                else
                {
                    Log.d("msg", "Inside onClick else");
                    button1.setBackgroundColor(R.color.color_green);
                    //                      button1.setBackgroundColor(Color.CYAN);
                    //                      flag=1;
                }
            }
        });

    }

2 个答案:

答案 0 :(得分:0)

试试这个

button1.setOnClickListener(new OnClickListener() {
        ColorDrawable buttonColor = (ColorDrawable) button1.getBackground();
        int colorId = buttonColor.getColor();
        @Override
        public void onClick(View v) {


            Log.d("msg", "Inside onClick");
            if(colorId==R.color.color_green)
            {
                Log.d("msg", "Inside onClick1 if"); 
                button1.getBackground().setColorFilter(Color.RED,PorterDuff.Mode.MULTIPLY);
                //Toast.makeText(MainActivity.this, " "+currentPostion1+" ", Toast.LENGTH_SHORT).show();
                //flag=0;
            }
            else
            {
                Log.d("msg", "Inside onClick else");
                button1.getBackground().setColorFilter(Color.GREEN,PorterDuff.Mode.MULTIPLY);
                //                      flag=1;
            }
        }
    });

答案 1 :(得分:0)

尝试使用布尔来实现它,

//global variable
boolean click=true;

if(click){
//first click
//change color
click = false;
}else{
//second click
//change color
click = true;
}