如何以编程方式将其他信息附加到按钮?我可以使用mButton.setText("新文本")来更改文本,但我希望能够添加更多字段,这样当您单击按钮时,您可以获取这些额外字段并使用数据.我怎么能这样做?
使用View.setTag(int key,Object tag).
您可以稍后使用getTag(int key)检索它.
在你的xml中:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item type="id"
name="string_key" />
<item type="id"
name="boolean_key" />
</resources>
Run Code Online (Sandbox Code Playgroud)
在你的代码中:
//I'd like to store String s and Boolean b in the button.
button.setTag(R.id.string_key, s);
button.setTag(R.id.boolean_key, b);
//Now, I'd like to retrieve the data in new fields.
String new_s = (String) button.getTag(R.id.string_key);
Boolean new_b = (Boolean) button.getTag(R.id.boolean_key);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2990 次 |
| 最近记录: |