我想知道如何更改模仿另一个按钮的背景资源的按钮backgroundresource,这样每当我更改该按钮的backgroundresource时,另一个按钮会模仿第一个按钮的外观......
例如:
int icon = R.drawable.ic_icon; //more specifically I stored R.drawable.ic_icon in SQL and retrieve and save in int icon when retrieve from that table, so when the table is change the first button dynamically change on create;
btn_01.setBackgroundResource(icon); //when this button is pressed it inflates a layout containing btn_02
btn_02.setBackgrounResource(??????); //this button is on a different layout and is used by different activity and should take the backgroundresource of the button that have been pressed to call that layout.
我可以使用if else语句,但我有不同的按钮可以被第二个按钮复制,每个按钮都有不同的背景资源可能性。
答案 0 :(得分:0)
我无法完全理解你的问题,但无论如何这里是我得到的
单击 Btn_01 时,您可以在开始活动之前将资源ID放入Intent。
Btn_01.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(this,Activity2.class);
intent.putExtra("resource_key", R.drawable.ic_heart);
startActivity(intent);
}
});
然后在您的Activity2中,您可以获取资源数据并设置为您想要的任何按钮
int DEFAULT_RESOURCE = R.drawable.ic_close;
int resourceId = getIntent().getIntExtra("resource_key",DEFAULT_RESOURCE);
Btn_02.setBackgroundResource(resourceId);