gpt4 book ai didi

java - 将 onClickListener 设置为大量 View

转载 作者:行者123 更新时间:2023-12-01 06:23:11 24 4
gpt4 key购买 nike

我有很多 View (大约30个,可能会更多),比如卡片。我为每个人设置了 onClickListener,但它不起作用。没有反应。这是我的代码:

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
final EditText searchProfile = (EditText) findViewById(R.id.search_profile);
final Button btnSearch = (Button) findViewById(R.id.btn_search);
final LinearLayout layoutProfileLeft = (LinearLayout) findViewById(R.id.layout_profiles_left);
searchProfile.setHint("UserName...");
btnSearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String profileName = searchProfile.getText().toString();
urlForProfile = "http://www.dotabuff.com/search?utf8=&q=" + searchProfile.getText().toString().replace(" ", "+") + "&commit=Search";
while (htmlSrc == "" || htmlSrc == null) {
setHtmlSrc(urlForProfile);
}
ArrayList<Integer> ids = new ArrayList<Integer>();
for (int i = 0; i < 30; i++) {
searchedHeroes.add(i, new SearchedHero(i));
RelativeLayout l = new RelativeLayout(StartActivity.this);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(-1,-2);
rlp.setMargins(8, 0, 8, 8);
l.setLayoutParams(rlp);
l.setBackgroundColor(Color.rgb(69,90,100));
l.setPadding(8,0,8,8);
l.setId(i);
//l.setLayoutParams(LinearLayout.);
ImageView img = new ImageView(StartActivity.this);
new DownloadImageTask(img).execute(searchedHeroes.get(i).getImageSrc());
img.setOnClickListener(this);
img.setId(i + 40);
l.addView(img);
RelativeLayout innerL = new RelativeLayout(StartActivity.this);
RelativeLayout.LayoutParams paramsForInnerL = new RelativeLayout.LayoutParams
(-2, -2);
paramsForInnerL.addRule(RelativeLayout.RIGHT_OF, img.getId());
TextView txtProfileName = new TextView(StartActivity.this);
txtProfileName.setText(searchedHeroes.get(i).getName());
txtProfileName.setId(i + 80);
TextView txtLastMatch = new TextView(StartActivity.this);
txtLastMatch.setText(searchedHeroes.get(i).getLastMatch());
txtProfileName.setTextSize(14);
txtProfileName.setTextColor(Color.WHITE);
txtLastMatch.setId(i + 120);
RelativeLayout.LayoutParams paramsForNameLastMatch = new RelativeLayout.LayoutParams
(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
innerL.addView(txtProfileName, rlp);
paramsForNameLastMatch.addRule(RelativeLayout.BELOW, txtLastMatch.getId());
innerL.addView(txtLastMatch, paramsForNameLastMatch);
l.addView(innerL, paramsForInnerL);
l.setOnClickListener(this);
ids.add(i, l.getId());
layoutProfileLeft.addView(l, rlp);
//}
//m.find();
//passUrl = "http://www.dotabuff.com" + m.group(1);
//Intent pass = new Intent(StartActivity.this, MainActivity.class);
//pass.putExtra("EmpID", searchedHeroes.get(0).getProfileUrl());
//startActivity(pass);
//}
//}
}
}
});
}

@Override
public void onClick(View v) {
Intent pass = new Intent(StartActivity.this, MainActivity.class);
pass.putExtra("Link", searchedHeroes.get(v.getId()).getProfileUrl());
startActivity(pass);
}

可能,每个 View 都必须 Intent 其他 Activity 。谢谢。

最佳答案

在我看来,您忘记将 View 设置为可点击:view.setClickable(true)

关于java - 将 onClickListener 设置为大量 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36561692/

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