简体   繁体   中英

get image button by tag id

I have image button with different tag ids.

ImageButton mFavorite = (ImageButton) convertView.findViewById(R.id.method_fav_btn);
mFavorite.setTag(pm.getId());

On click I want to identify which image button was clicked. and on the basis of that i want to change it's image.

mFavorite.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // Change images:
        mFavorite.setImageResource(R.drawable.ic_action_important);
    }
});

But, it's not changing. How can I identify button by it's tag id?

Make your Activity implement View.OnClickListener :

public class example extends Activity implements View.OnClickListener {

    public example() {
        ImageButton b1 = new ImageButton(this);
        ImageButton b2 = new ImageButton(this);
        ImageButton b3 = new ImageButton(this);

        b1.setTag("b1");
        b2.setTag("b2");
        b3.setTag("b3");

        b1.setOnClickListener(this);
        b2.setOnClickListener(this);
        b3.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        String tag = String.valueOf(view.getTag());

        if (tag == "b1") {
            // B1 was clicked
        } else if (tag == "b2") {
            // b2 was clicked
        } else if (tag == "b3") {
            // b3 was clicked
        }
    }
}

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