我花了大约3天时间寻找这个答案,最后我决定自己创建这个问题。
基本上我有一个带有两个微调器的活动。一种是头发的类型,另一种是头发的颜色。你知道我要去哪里。我希望能够根据所选的两个项目改变图片(在头上)。
如果我在这里没有意义,那就是一个例子:
但第3步不起作用!!!
这段代码太长了,但大部分都是重复的东西,所以不用太长时间才能理解:)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.head_zoom);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Spinner hairSpinner = (Spinner) findViewById(R.id.hairSpinner);
hairSpinner.setOnItemSelectedListener(this);
List<String> hairTypeArray = new ArrayList<String>();
hairTypeArray.add("Bald");
hairTypeArray.add("Female Short");
hairTypeArray.add("Female Medium");
hairTypeArray.add("Female Long");
hairTypeArray.add("Male Short");
hairTypeArray.add("Male Medium");
hairTypeArray.add("Male Long");
ArrayAdapter<String> hairTypeAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, hairTypeArray);
hairTypeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
hairSpinner.setAdapter(hairTypeAdapter);
Spinner hairColor = (Spinner) findViewById(R.id.hairColor);
hairColor.setOnItemSelectedListener(this);
List<String> hairColorArray = new ArrayList<>();
hairColorArray.add("Black");
hairColorArray.add("Blonde");
hairColorArray.add("Blue");
hairColorArray.add("Brown");
hairColorArray.add("Ginger");
hairColorArray.add("Green");
hairColorArray.add("Red");
ArrayAdapter<String> hairColorAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, hairColorArray);
hairColorAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
hairColor.setAdapter(hairColorAdapter);
}
public void onItemSelected(AdapterView<?> parent, View view, int position, long id){
String item = parent.getItemAtPosition(position).toString();
String type = "Bald", color = "Black";
//Toast.makeText(parent.getContext(), "Selected: "+item, Toast.LENGTH_LONG).show();
Spinner spinner = (Spinner) parent;
if(spinner.getId() == R.id.hairSpinner) {
type = item;
}
else {
color = item;
}
final ImageView imageView = (ImageView)findViewById(R.id.head);
switch(type){
case "Bald":
imageView.setImageResource((R.drawable.sw_head));
break;
case "Female Short":
if(color=="Black")
imageView.setImageResource(R.drawable.fhair_short_black);
else if(color=="Blonde")
imageView.setImageResource(R.drawable.fhair_short_blonde);
else if(color=="Blue")
imageView.setImageResource(R.drawable.fhair_short_blue);
else if(color=="Brown")
imageView.setImageResource(R.drawable.fhair_short_brown);
else if(color=="Ginger")
imageView.setImageResource(R.drawable.fhair_short_ginger);
else if(color=="Green")
imageView.setImageResource(R.drawable.fhair_short_green);
else if(color=="Red")
imageView.setImageResource(R.drawable.fhair_short_red);
break;
case "Female Medium":
if(color=="Black")
imageView.setImageResource(R.drawable.fhair_med_black);
else if(color=="Blonde")
imageView.setImageResource(R.drawable.fhair_med_blonde);
else if(color=="Blue")
imageView.setImageResource(R.drawable.fhair_med_blue);
else if(color=="Brown")
imageView.setImageResource(R.drawable.fhair_med_brown);
else if(color=="Ginger")
imageView.setImageResource(R.drawable.fhair_med_ginger);
else if(color=="Green")
imageView.setImageResource(R.drawable.fhair_med_green);
else if(color=="Red")
imageView.setImageResource(R.drawable.fhair_med_red);
break;
case "Female Long":
if(color=="Black")
imageView.setImageResource(R.drawable.fhair_long_black);
else if(color=="Blonde")
imageView.setImageResource(R.drawable.fhair_long_blonde);
else if(color=="Blue")
imageView.setImageResource(R.drawable.fhair_long_blue);
else if(color=="Brown")
imageView.setImageResource(R.drawable.fhair_long_brown);
else if(color=="Ginger")
imageView.setImageResource(R.drawable.fhair_long_ginger);
else if(color=="Green")
imageView.setImageResource(R.drawable.fhair_long_green);
else if(color=="Red")
imageView.setImageResource(R.drawable.fhair_long_red);
break;
case "Male Short":
if(color=="Black")
imageView.setImageResource(R.drawable.mhair_short_black);
else if(color=="Blonde")
imageView.setImageResource(R.drawable.mhair_short_blonde);
else if(color=="Blue")
imageView.setImageResource(R.drawable.mhair_short_blue);
else if(color=="Brown")
imageView.setImageResource(R.drawable.mhair_short_brown);
else if(color=="Ginger")
imageView.setImageResource(R.drawable.mhair_short_ginger);
else if(color=="Green")
imageView.setImageResource(R.drawable.mhair_short_green);
else if(color=="Red")
imageView.setImageResource(R.drawable.mhair_short_red);
break;
case "Male Medium":
if(color=="Black")
imageView.setImageResource(R.drawable.mhair_med_black);
else if(color=="Blonde")
imageView.setImageResource(R.drawable.mhair_med_blonde);
else if(color=="Blue")
imageView.setImageResource(R.drawable.mhair_med_blue);
else if(color=="Brown")
imageView.setImageResource(R.drawable.mhair_med_brown);
else if(color=="Ginger")
imageView.setImageResource(R.drawable.mhair_med_ginger);
else if(color=="Green")
imageView.setImageResource(R.drawable.mhair_med_green);
else if(color=="Red")
imageView.setImageResource(R.drawable.mhair_med_red);
break;
case "Male Long":
if(color=="Black")
imageView.setImageResource(R.drawable.mhair_long_black);
else if(color=="Blonde")
imageView.setImageResource(R.drawable.mhair_long_blonde);
else if(color=="Blue")
imageView.setImageResource(R.drawable.mhair_long_blue);
else if(color=="Brown")
imageView.setImageResource(R.drawable.mhair_long_brown);
else if(color=="Ginger")
imageView.setImageResource(R.drawable.mhair_long_ginger);
else if(color=="Green")
imageView.setImageResource(R.drawable.mhair_long_green);
else if(color=="Red")
imageView.setImageResource(R.drawable.mhair_long_red);
break;
}
}
public void onNothingSelected(AdapterView<?> parent){
}
问题在于,无论何时从旋转器中选择一个项目,它都会激活侦听器,但不会考虑其他微调器。因此,如果我改变头发颜色,它会因为
而重置为光头 String type = "Bald", color = "Black";
感谢您给予的任何帮助,谢谢。
答案 0 :(得分:2)
你必须将开关分成两个不同的开关,一个用于hairtype,另一个用于haircolor。你现在正在做的是获得hairtype和休息,永远不会出现颜色方面。
if (view == hairSpinner){
switch (item){
case "Bald":
case "Female Short":
case "Female Medium":
case "Female Long":
case "Male Short":
case "Male Medium":
case "Male Long":
type = item;
break;
}
}
else{
switch (item){
case "Black":
case "Blonde":
case "Blue":
case "Brown":
case "Ginger":
case "Green":
case "Red":
color = item;
break;
}
}
答案 1 :(得分:1)
杰拉尔多的回答是你的正确答案。但是,由于你有很多类型和颜色,这将是非常长的,并且它不会非常可定制。
如果我是你,我会尝试为该类型(只有形状,没有颜色)和颜色的drawables创建drawables。在这种情况下,您可以轻松地将颜色应用于类型,并键入颜色,只需两个简单的变量即可识别当前类型和当前颜色。
答案 2 :(得分:1)
String type=null;
String color=null;
if (view==hairSpinner)
type = parent.getItemAtPosition(position).toString();
else
color = parent.getItemAtPosition(position).toString();
答案 3 :(得分:1)
我的意思是这样的:
public class Test
extends AppCompatActivity
implements OnItemSelectedListener{
private Spinner hairSpinner;
private Spinner hairColor;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
hairSpinner = (Spinner) findViewById(R.id.hairSpinner);
...
hairColor = (Spinner) findViewById(R.id.hairColor);
...
}
public void onItemSelected(AdapterView<?> parent, View view, int position, long id){
String type=null;
String color=null;
if (view==hairSpinner)
type = parent.getItemAtPosition(position).toString();
else
color = parent.getItemAtPosition(position).toString();
}
}
答案 4 :(得分:0)
这次应该有效.....
public class MainActivity
extends AppCompatActivity
implements OnItemSelectedListener{
private Spinner hairSpinner;
private Spinner hairColor;
private String type;
private String color;
private ImageView imageView;
private List<String> hairTypeArray = new ArrayList<String>();
private List<String> hairColorArray = new ArrayList<String>();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.head_zoom);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
imageView = (ImageView)findViewById(R.id.head);
type="Bald";
hairSpinner = (Spinner) findViewById(R.id.hairSpinner);
hairSpinner.setOnItemSelectedListener(this);
hairTypeArray.add("Bald");
hairTypeArray.add("Female Short");
hairTypeArray.add("Female Medium");
hairTypeArray.add("Female Long");
hairTypeArray.add("Male Short");
hairTypeArray.add("Male Medium");
hairTypeArray.add("Male Long");
ArrayAdapter<String> hairTypeAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, hairTypeArray);
hairTypeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
hairSpinner.setAdapter(hairTypeAdapter);
color = "Black";
hairColor = (Spinner) findViewById(R.id.hairColor);
hairColor.setOnItemSelectedListener(this);
hairColorArray.add("Black");
hairColorArray.add("Blonde");
hairColorArray.add("Blue");
hairColorArray.add("Brown");
hairColorArray.add("Ginger");
hairColorArray.add("Green");
hairColorArray.add("Red");
ArrayAdapter<String> hairColorAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, hairColorArray);
hairColorAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
hairColor.setAdapter(hairColorAdapter);
}
public void onItemSelected(AdapterView<?> parent, View view, int position, long id){
if (parent.getId()==hairSpinner.getId())
type = hairTypeArray.get(position);
else
color = hairColorArray.get(position);
switch(type){
case "Bald":
imageView.setImageResource((R.drawable.sw_head));
break;
case "Female Short":
if(color=="Black")
imageView.setImageResource(R.drawable.fhair_short_black);
else if(color=="Blonde")
imageView.setImageResource(R.drawable.fhair_short_blonde);
else if(color=="Blue")
imageView.setImageResource(R.drawable.fhair_short_blue);
else if(color=="Brown")
imageView.setImageResource(R.drawable.fhair_short_brown);
else if(color=="Ginger")
imageView.setImageResource(R.drawable.fhair_short_ginger);
else if(color=="Green")
imageView.setImageResource(R.drawable.fhair_short_green);
else if(color=="Red")
imageView.setImageResource(R.drawable.fhair_short_red);
break;
case "Female Medium":
if(color=="Black")
imageView.setImageResource(R.drawable.fhair_med_black);
else if(color=="Blonde")
imageView.setImageResource(R.drawable.fhair_med_blonde);
else if(color=="Blue")
imageView.setImageResource(R.drawable.fhair_med_blue);
else if(color=="Brown")
imageView.setImageResource(R.drawable.fhair_med_brown);
else if(color=="Ginger")
imageView.setImageResource(R.drawable.fhair_med_ginger);
else if(color=="Green")
imageView.setImageResource(R.drawable.fhair_med_green);
else if(color=="Red")
imageView.setImageResource(R.drawable.fhair_med_red);
break;
case "Female Long":
if(color=="Black")
imageView.setImageResource(R.drawable.fhair_long_black);
else if(color=="Blonde")
imageView.setImageResource(R.drawable.fhair_long_blonde);
else if(color=="Blue")
imageView.setImageResource(R.drawable.fhair_long_blue);
else if(color=="Brown")
imageView.setImageResource(R.drawable.fhair_long_brown);
else if(color=="Ginger")
imageView.setImageResource(R.drawable.fhair_long_ginger);
else if(color=="Green")
imageView.setImageResource(R.drawable.fhair_long_green);
else if(color=="Red")
imageView.setImageResource(R.drawable.fhair_long_red);
break;
case "Male Short":
if(color=="Black")
imageView.setImageResource(R.drawable.mhair_short_black);
else if(color=="Blonde")
imageView.setImageResource(R.drawable.mhair_short_blonde);
else if(color=="Blue")
imageView.setImageResource(R.drawable.mhair_short_blue);
else if(color=="Brown")
imageView.setImageResource(R.drawable.mhair_short_brown);
else if(color=="Ginger")
imageView.setImageResource(R.drawable.mhair_short_ginger);
else if(color=="Green")
imageView.setImageResource(R.drawable.mhair_short_green);
else if(color=="Red")
imageView.setImageResource(R.drawable.mhair_short_red);
break;
case "Male Medium":
if(color=="Black")
imageView.setImageResource(R.drawable.mhair_med_black);
else if(color=="Blonde")
imageView.setImageResource(R.drawable.mhair_med_blonde);
else if(color=="Blue")
imageView.setImageResource(R.drawable.mhair_med_blue);
else if(color=="Brown")
imageView.setImageResource(R.drawable.mhair_med_brown);
else if(color=="Ginger")
imageView.setImageResource(R.drawable.mhair_med_ginger);
else if(color=="Green")
imageView.setImageResource(R.drawable.mhair_med_green);
else if(color=="Red")
imageView.setImageResource(R.drawable.mhair_med_red);
break;
case "Male Long":
if(color=="Black")
imageView.setImageResource(R.drawable.mhair_long_black);
else if(color=="Blonde")
imageView.setImageResource(R.drawable.mhair_long_blonde);
else if(color=="Blue")
imageView.setImageResource(R.drawable.mhair_long_blue);
else if(color=="Brown")
imageView.setImageResource(R.drawable.mhair_long_brown);
else if(color=="Ginger")
imageView.setImageResource(R.drawable.mhair_long_ginger);
else if(color=="Green")
imageView.setImageResource(R.drawable.mhair_long_green);
else if(color=="Red")
imageView.setImageResource(R.drawable.mhair_long_red);
break;
}
}
public void onNothingSelected(AdapterView<?> parent){
}
}
答案 5 :(得分:0)
解决:问题最终是我已经两次初始化列表,一次是全局的,一次是onCreate。我从onCreate中删除了此内容,并将color=="black"
更改为color.equals("black")
。
这是我现在的代码:
public class HeadZoom extends MainActivity implements AdapterView.OnItemSelectedListener{
private Spinner hairSpinner;
private Spinner hairColor;
private String type;
private String color;
private ImageView imageView;
private List<String> hairTypeArray = new ArrayList<String>();
private List<String> hairColorArray = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.head_zoom);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
imageView = (ImageView) findViewById(R.id.head);
type="Bald";
hairSpinner = (Spinner) findViewById(R.id.hairSpinner);
hairSpinner.setOnItemSelectedListener(this);
hairTypeArray.add("Bald");
hairTypeArray.add("Female Short");
hairTypeArray.add("Female Medium");
(...)
ArrayAdapter<String> hairTypeAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, hairTypeArray);
hairTypeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
hairSpinner.setAdapter(hairTypeAdapter);
color = "Black";
hairColor = (Spinner) findViewById(R.id.hairColor);
hairColor.setOnItemSelectedListener(this);
hairColorArray.add("Black");
hairColorArray.add("Blonde");
hairColorArray.add("Blue");
(...)
ArrayAdapter<String> hairColorAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, hairColorArray);
hairColorAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
hairColor.setAdapter(hairColorAdapter);
}
public void onItemSelected(AdapterView<?> parent, View view, int position, long id){
if(parent.getId()==hairSpinner.getId())
type = hairTypeArray.get(position);
else
color = hairColorArray.get(position);
switch(type){
case "Bald":
imageView.setImageResource((R.drawable.sw_head));
break;
case "Female Short":
if(color.equals("Black"))
imageView.setImageResource(R.drawable.fhair_short_black);
else if(color.equals("Blonde"))
imageView.setImageResource(R.drawable.fhair_short_blonde);
else if(color.equals("Blue"))
imageView.setImageResource(R.drawable.fhair_short_blue);
else if(color.equals("Brown"))
(...)
break;
case "Female Medium":
if(color.equals("Black"))
imageView.setImageResource(R.drawable.fhair_med_black);
else if(color.equals("Blonde"))
imageView.setImageResource(R.drawable.fhair_med_blonde);
else if(color.equals("Blue"))
imageView.setImageResource(R.drawable.fhair_med_blue);
else if(color.equals("Brown"))
(...)
break;
case "Female Long":
(...)
default:
break;
}
}
public void onNothingSelected(AdapterView<?> parent){
}
谢谢大家的帮助。