在Android中的Scrollview中获取ImageView的ID

时间:2011-11-25 14:42:36

标签: android imageview scrollview

如果在scrollView中找到此元素,如何获取已单击的元素的id?在我的情况下,我有4张照片(ImageViews),如果我点击其中一张,我想得到它的 id (已经使用ImageView.setId(int)设置)。这是我的代码:

package scroll.s;

import android.app.Activity;
import android.os.Bundle;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ScrollsActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        int layoutId = 0;  

        RelativeLayout main = new RelativeLayout(this);
        RelativeLayout.LayoutParams mainParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        main.setId(layoutId);
        layoutId++;

        TextView past = new TextView (this);
        RelativeLayout.LayoutParams pastParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        past.setId(layoutId);
        layoutId++;
        past.setText("Past: ");
        pastParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        main.addView(past, pastParams);

        final HorizontalScrollView hsv = new HorizontalScrollView(this);
        hsv.setId(layoutId);
        layoutId++;
        hsv.setHorizontalScrollBarEnabled(false);
        mainParams.addRule(RelativeLayout.BELOW, 1);
        main.addView(hsv, mainParams);        

        final RelativeLayout relative1 = new RelativeLayout(this);
        relative1.setId(layoutId);
        layoutId++;
        hsv.addView(relative1);

        for (int i=0; i<4; i++) {
            ImageView current = new ImageView(this);
            current.setBackgroundDrawable(getResources().getDrawable(R.drawable.example));
            current.setId(layoutId);

            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            params.addRule(RelativeLayout.RIGHT_OF, (layoutId-1) );
            params.leftMargin=10;
            relative1.addView(current, params);
            layoutId++;
        }

        for (int i=0; i<4; i++) {
            TextView currentText = new TextView(this);
            currentText.setText("random text");
            currentText.setId(layoutId);

            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            params.addRule(RelativeLayout.BELOW, (layoutId-4) );
            params.addRule(RelativeLayout.ALIGN_LEFT, (layoutId-4) );
            params.topMargin=5;
            relative1.addView(currentText, params);
            layoutId++;
        }


        this.setContentView(main);   
    }
}

以下是我使用此代码实现的目标:scrollsview

我得到错误/ AndroidRuntime(2258):android.content.res.Resources $ NotFoundException:字符串资源ID#0x4 当我输入以下代码时:

    current.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getBaseContext(), current.getId(), Toast.LENGTH_SHORT).show();
        }
    });

2 个答案:

答案 0 :(得分:2)

将其更改为:

 current.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getBaseContext(), String.valueOf(v.getId()), Toast.LENGTH_SHORT).show();
            }
        });

答案 1 :(得分:1)

方法makeText()的第二个参数是字符串,而不是 Int current.getId()),因此您应该将其转换为这样String

current.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getBaseContext(),""+v.getId(), Toast.LENGTH_SHORT).show();
            }
        });