gpt4 book ai didi

android - 在 Android 的 Scrollview 中获取 ImageView 的 id

转载 作者:搜寻专家 更新时间:2023-11-01 09:11:07 26 4
gpt4 key购买 nike

如果在 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

我得到了ERROR/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();
}
});

关于android - 在 Android 的 Scrollview 中获取 ImageView 的 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8270660/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com