[英]How to set ImageButton size and when button is pressed show it on added image
[英]How to change the image of an ImageButton when the user click the button
我花了3个小时试图制作一个ImageButton来在用户单击时更改其Image,但这对我来说是不可能的。 提前致谢!
主队
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton dado_btn = (ImageButton) findViewById (R.id.dado_button);
replace = R.drawable.face_1;
dado_btn.setBackgroundResource(replace);
}
public void OnClickTirada (View v){
//this doesn't work at all
dado_btn.setImageDrawable(getResources().getDrawable(R.drawable.face_1));
}
dado_btn
仅在onCreate()
本地定义,因此您不能在任何其他方法中使用它。
为了完成这项工作,您必须声明一个字段dado_btn
,如果代码可以编译的话,可能已经完成了。 第二步,不要在onCreate()
声明局部变量。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dado_btn = (ImageButton) findViewById (R.id.dado_button);
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.