문제

I've stored the imagebutton info in an xml. It's image path,width,height and so on. But since there are multiple images I want to store data for each imagebutton while it's clicked. for example, if there are two buttons named male and female, i want to set String value = "male"; if male image button is clicked. But it did not work. Can somebody help me?

 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 is the image button id which is created in xml file. AlertBox text does not show "female" for this example.

도움이 되었습니까?

해결책

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);
            }
        });
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top