如果在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);
    }
}

以下是我使用此代码实现的目标:
我得到错误/androidruntime(2258):android.content.res.resources$notfoundexception:string resource id 0x4
当我输入以下代码时:
    current.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getBaseContext(), current.getId(), Toast.LENGTH_SHORT).show();
        }
    });

最佳答案

更改为:

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

08-05 18:41
查看更多