按标签ID获取图片按钮

时间:2015-01-14 19:49:42

标签: java android

我有不同标签ID的图像按钮。

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

点击后我想确定点击了哪个图像按钮。并在此基础上,我想改变它的形象。

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

但是,它并没有改变。如何通过标签ID识别按钮?

1 个答案:

答案 0 :(得分:0)

制作Activity工具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
        }
    }
}