Hi i am new to android,I am using spinner background image. The spinner contains four values, and each one has images.If i click the spinner values, the images will be changed in the spinner background. Please help me how can i change the background image when the spinner value is changed?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Give Id to the parent (Linear or relative) layout of your main.xml file and in the java file,if the four values of your spinner is Mercury,Venus,Earth,Mars, then in the Onselected itemlistener,put code like this
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Toast.makeText(parent.getContext(), "The planet is " +
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
if(parent.getItemAtPosition(pos).toString().equalsIgnoreCase("Mercury"))
{
ll1.setBackgroundResource(R.drawable.java_j2me);
}
if(parent.getItemAtPosition(pos).toString().equalsIgnoreCase("Venus"))
{
ll1.setBackgroundResource(R.drawable.image);
}
if(parent.getItemAtPosition(pos).toString().equalsIgnoreCase("Earth"))
{
ll1.setBackgroundResource(R.drawable.javaandroid);
}
if(parent.getItemAtPosition(pos).toString().equalsIgnoreCase("Mars"))
{
ll1.setBackgroundResource(R.drawable.party);
}
}