【发布时间】:2013-12-26 11:06:16
【问题描述】:
我有 10 个按钮,我想为所有按钮设置 OnClickListener。此外,单击任何按钮应用程序将进行另一项活动。我只在活动类上发布按钮的定义。我的代码;
Button button1 = (Button) findViewById(R.id.button1);
Button button2 = (Button) findViewById(R.id.button2);
Button button3 = (Button) findViewById(R.id.button3);
Button button4 = (Button) findViewById(R.id.button4);
Button button5 = (Button) findViewById(R.id.button5);
Button button6 = (Button) findViewById(R.id.button6);
Button button7 = (Button) findViewById(R.id.button7);
Button button8 = (Button) findViewById(R.id.button8);
Button button9 = (Button) findViewById(R.id.button9);
Button button10 = (Button) findViewById(R.id.button10);
Button buttons[] = {button1, button2, button3, button4, button5, button6, button7, button8, button9, button10};
final String urlOfButtons[] = {"","","","","","","","","","",""};
final String titles[] = {"","","","","","","","","","",""};
JsonNode itemNode = jsonNode.path("Docs");
for(int i=0 ; i<itemNode.size() ; i++){
titles[i] = itemNode.get(i).path("Text").asText();
title = titles[i];
buttons[i].setText(title);
urlOfButtons[i] = itemNode.get(i).path("Link").asText();
url = urlOfButtons[i];
buttons[i].setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(HouseDetail.this, HouseDetailPdf.class);
intent.putExtra("title", title);
intent.putExtra("url", url);
startActivity(intent);
}
});
}
这通常只取 title 和 url 的最后一个值。字符串 url,标题定义在类的顶部。我单击的每个按钮都将相同的值转到其他活动。我想要 10 个不同的值来添加额外的值。所以我想添加而不是标题和网址;标题[i] 和 url[i] 作为额外的。我希望我很清楚。
【问题讨论】:
-
您是通过编程方式还是在 xml 布局中添加这些按钮?
-
我当然会在布局中添加这些按钮。
标签: android buttonclick