机器人的ImageButton改变图像的OnClick [英] android Imagebutton change Image OnClick
问题描述
我只是增加了一个新的绘制文件夹下的资源文件夹中。在绘制文件夹复制我从绘制,华电国际文件夹中的ic_launcher.png文件。 我想通过新修改的标准ImageButton的图像时,我preSS的按钮。我写了一些code,但是当我启动应用程序崩溃。
按钮imgButton;
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
findViewById(R.id.imgButton).setOnClickListener(imgButtonHandler);
}
View.OnClickListener imgButtonHandler =新View.OnClickListener(){
公共无效的onClick(视图v){
imgButton.setBackgroundResource(R.drawable.ic_launcher);
}
};
编辑:我改变了这一点,这也不能作品
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
imgButton =(按钮)findViewById(R.id.imgButton);
imgButton.setOnClickListener(imgButtonHandler);
}
View.OnClickListener imgButtonHandler =新View.OnClickListener(){
公共无效的onClick(视图v){
imgButton.setBackgroundResource(R.drawable.ic_launcher);
}
};
编辑2:这个工程。感谢所有。
的ImageButton按钮;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
按钮=(的ImageButton)findViewById(R.id.imgButton);
button.setOnClickListener(imgButtonHandler);
}
View.OnClickListener imgButtonHandler =新View.OnClickListener(){
公共无效的onClick(视图v){
button.setBackgroundResource(R.drawable.ic_launcher);
}
};
本的误导我一点 - 它应该是setImageResource,而不是setBackgroundResource :) !!
以下工作正常:
的ImageButton BTN =(的ImageButton)findViewById(R.id.imageButton1);
btn.setImageResource(R.drawable.actions_record);
在使用 setBackgroundResource
实际的ImageButton的形象,同时
住宿而背景图像改变巫婆导致难看的ImageButton对象
感谢。
i just added a new drawable folder under res folder. In the drawable folder i copied the ic_launcher.png file from drawable-hdpi folder. I wanna change the standard ImageButton image through the new one when i press the button. I wrote some code, but when i start the app it crashes.
Button imgButton;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.imgButton).setOnClickListener(imgButtonHandler);
}
View.OnClickListener imgButtonHandler = new View.OnClickListener() {
public void onClick(View v) {
imgButton.setBackgroundResource(R.drawable.ic_launcher);
}
};
EDIT: I changed to this, and this also not works.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgButton = (Button) findViewById(R.id.imgButton);
imgButton.setOnClickListener(imgButtonHandler);
}
View.OnClickListener imgButtonHandler = new View.OnClickListener() {
public void onClick(View v) {
imgButton.setBackgroundResource(R.drawable.ic_launcher);
}
};
EDIT 2: THIS WORKS. Thanks to all.
ImageButton button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button= (ImageButton)findViewById(R.id.imgButton);
button.setOnClickListener(imgButtonHandler);
}
View.OnClickListener imgButtonHandler = new View.OnClickListener() {
public void onClick(View v) {
button.setBackgroundResource(R.drawable.ic_launcher);
}
};
this misleaded me a bit - it should be "setImageResource" instead of "setBackgroundResource" :) !!
The following works fine :
ImageButton btn = (ImageButton)findViewById(R.id.imageButton1);
btn.setImageResource(R.drawable.actions_record);
while when using the setBackgroundResource
the actual imagebutton's image
stays while the background image is changed witch leads to a ugly looking imagebutton object
Thanks.
这篇关于机器人的ImageButton改变图像的OnClick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!