ImageButton单击动作Android

时间:2014-04-21 20:29:47

标签: android click imagebutton

我已将图像按钮信息存储在xml中。它的图像路径,宽度,高度等。 但是,由于有多个图像,我想在点击时为每个图像按钮存储数据。例如,如果有两个名为male和female的按钮,我想设置String value =“male”;如果单击男性图像按钮。但它没有用。有人可以帮帮我吗?

 public void AddAllImageButtons() throws IOException {
        AbsoluteLayout Layout = (AbsoluteLayout) findViewById(R.id.layout1);
        ImageButton btn = new ImageButton(this);
        Layout.addView(btn);
        AbsoluteLayout.LayoutParams absParams = (AbsoluteLayout.LayoutParams) btn
                .getLayoutParams();

        absParams.x = x;
        absParams.y = y;
        btn.setLayoutParams(absParams);
        btn.setBackgroundDrawable(null);
        btn.setImageBitmap(BitmapFactory.decodeStream(getAssets().open(path)));

        if(type.equals("imagebutton")){
        if(elementId.equals("female")){
        btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                gender = elementId;
                GenerateAlertBoxes(gender);
            }
        });
}

Id是在xml文件中创建的图像按钮ID。  对于此示例,AlertBox文本不显示“女性”。

1 个答案:

答案 0 :(得分:0)

you can setTag("value") to set the value in the ImageButton btn and on the onclick event you can use getTag() function to retrieve the value and set it to gender.

use btn.setTag(elementid); whenever you add a btn to the layout.......

inside btn.setOnClickListener you can write v.getTag().toString() to get the value and set it to gender...... 

btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                gender = v.getTag().toString();
                GenerateAlertBoxes(gender);
            }
        });