Android - 更改视图背景图像onclick [英] Android - Change view background image onclick

查看:155
本文介绍了Android - 更改视图背景图像onclick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试更改视图的背景图片onclick,这是代码:



Hi,
I am trying to change the background image of a view onclick, here is the code:

public class ScreenChanger extends Activity{
	
	private ImageButton imagebutton;
	private ImageButton imagebuttonRED;
	private ImageButton imagebuttonBLUE;
	private ImageButton imagebuttonYELW;
	private ImageButton imagebuttonGRN;
	private View layout;
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.screenview);
	    imagebutton = (ImageButton) findViewById(R.id.imageButton1);
	    
	    imagebuttonRED = (ImageButton) findViewById(R.id.ImageButton04);
	    imagebuttonBLUE = (ImageButton) findViewById(R.id.ImageButton02);
	    imagebuttonYELW = (ImageButton) findViewById(R.id.ImageButton03);
	    imagebuttonGRN = (ImageButton) findViewById(R.id.ImageButton01);
	    
	    layout = (View) findViewById(R.layout.screenview);
	
	    imagebutton.setOnClickListener(new OnClickListener() {

	        public void onClick(View v) {
	            setContentView(R.layout.main);
	            Intent myIntent = new Intent(ScreenChanger.this, FlashLightActivity.class);
	            ScreenChanger.this.startActivity(myIntent);
	        }
	    });
	
	    imagebuttonRED.setOnClickListener(new OnClickListener() {

	        public void onClick(View v) {
	        	setContentView(layout);
	            layout.setBackgroundColor(drawable.red);
	        }
	    });
	    imagebuttonBLUE.setOnClickListener(new OnClickListener() {

	        public void onClick(View v) {
	        	setContentView(layout);
	            layout.setBackgroundColor(drawable.blue);
	        }
	    });
	    imagebuttonYELW.setOnClickListener(new OnClickListener() {

	        public void onClick(View v) {
	        	setContentView(layout);
	            layout.setBackgroundColor(drawable.yellow);
	        }
	    });
	    imagebuttonGRN.setOnClickListener(new OnClickListener() {

	        public void onClick(View v) {
	        	setContentView(layout);
	            layout.setBackgroundColor(drawable.green);
	        }
	    });
	    
}
}





我遇到的问题是当我点击按钮关闭我的按钮时app



The problem i have is when i click the button it closes my app

推荐答案

不要使用setContentview



制作你的布局ID

获取该ID并尝试使用它..
don''t use setContentview

make your layout id
get that id and try to work on it..


这篇关于Android - 更改视图背景图像onclick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆