![](/img/trans.png)
[英]How to assign an integer value to an imageView in Android Studio?
[英]Android how to assign a spinner position an integer value
好的,我有一个装有字符串的微调器,如何使用数组将值分配给整数?
例如微调器有
选择1后,如何根据选择初始化变量,使用if语句或switch / case?
我包含一些注释的代码来说明我要尝试的内容,在此示例中,我要填充一个称为“ actLevel”的整数。
public class spinActMultFunction implements OnItemSelectedListener {
@Override
public void onItemSelected(AdapterView<?> parent, View arg1, int pos, long id) {
String str=parent.getItemAtPosition(pos).toString();
activityMultiplier.setText(str);
/*
If (pos) = 1
then actLevel = 1.2
else if (pos) = 2
then actLevel = 1.6
etc..
*/
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
最终成功了:
public void onItemSelected(AdapterView<?> parent, View arg1, int pos,
long id) {
String str = parent.getItemAtPosition(pos).toString();
activityMultiplier.setText(str);
switch(pos){
case 0:
actLevel = 1.2;
break;
case 1:
actLevel = 1.3;
break;
case 2:
actLevel = 1.5;
break;
case 3:
actLevel = 1.7;
break;
case 4:
actLevel = 1.9;
break;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.