我的项目中有2个微调器,到目前为止,只有1个微调器正在工作!我正在尝试让2个微调器在选定时更改imageview中的图像!第一个微调器从strings.xml中的数组中获取数据,还希望第二个数组做同样的事情,并像第一个数组一样更新imageview!经过反复试验,我不知道该怎么做,并将代码编辑回一个微调器!
public class SpinnerRes extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final String[] spstr = getResources().getStringArray(R.array.spinnervalue);
final Spinner sp = (Spinner)findViewById(R.id.spinner1);
final ArrayAdapter<String> ar = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,spstr);
sp.setAdapter(ar);
sp.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
final ImageView im = (ImageView)findViewById(R.id.imageView1);
String s=((TextView)arg1).getText().toString();
if(s.equals("A"))
im.setImageDrawable(getResources().getDrawable(R.drawable.a));
if(s.equals("A6"))
im.setImageDrawable(getResources().getDrawable(R.drawable.a6));
if(s.equals("A7"))
im.setImageDrawable(getResources().getDrawable(R.drawable.a7));
if(s.equals("Am"))
im.setImageDrawable(getResources().getDrawable(R.drawable.am));
if(s.equals("Am6"))
im.setImageDrawable(getResources().getDrawable(R.drawable.am6));
if(s.equals("Am7"))
im.setImageDrawable(getResources().getDrawable(R.drawable.am7));
if(s.equals("Amaj7"))
im.setImageDrawable(getResources().getDrawable(R.drawable.amaj7));
if(s.equals("img8"))
im.setImageDrawable(getResources().getDrawable(R.drawable.img4));
if(s.equals("img9"))
im.setImageDrawable(getResources().getDrawable(R.drawable.img1));
if(s.equals("img10"))
im.setImageDrawable(getResources().getDrawable(R.drawable.img2));
if(s.equals("img11"))
im.setImageDrawable(getResources().getDrawable(R.drawable.img3));
if(s.equals("img12"))
im.setImageDrawable(getResources().getDrawable(R.drawable.img4));
if(s.equals("img13"))
im.setImageDrawable(getResources().getDrawable(R.drawable.img1));
if(s.equals("img14"))
im.setImageDrawable(getResources().getDrawable(R.drawable.img2));
if(s.equals("img15"))
im.setImageDrawable(getResources().getDrawable(R.drawable.img3));
if(s.equals("img16"))
im.setImageDrawable(getResources().getDrawable(R.drawable.img4));
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
}
带有2个微调框的xml。
<Spinner
android:layout_height="40dp"
android:layout_width="60dp"
android:id="@+id/spinner1"
android:layout_alignBottom="@+id/imageView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="31dp"
android:layout_marginStart="31dp">
</Spinner>
<Spinner
android:layout_height="40dp"
android:layout_width="50dp"
android:id="@+id/spinner2"
android:layout_alignTop="@+id/spinner1"
android:layout_toRightOf="@+id/spinner1"
android:layout_toEndOf="@+id/spinner1"
android:layout_marginLeft="18dp"
android:layout_marginStart="18dp">
</Spinner>
具有2个数组的strings.xml文件
<string-array name="spinnervalue">
<item>A</item>
<item>A6</item>
<item>A7</item>
<item>Am</item>
<item>Am6</item>
<item>Am7</item>
<item>Amaj7</item>
<item>img8</item>
<item>img9</item>
<item>img10</item>
<item>img11</item>
<item>img12</item>
<item>img13</item>
<item>img14</item>
<item>img15</item>
<item>img16</item>
</string-array>
<string-array name="spinnervalue2">
<item>A</item>
<item>A6</item>
<item>A7</item>
<item>Am</item>
<item>Am6</item>
<item>Am7</item>
<item>Amaj7</item>
<item>img8</item>
<item>img9</item>
<item>img10</item>
<item>img11</item>
<item>img12</item>
<item>img13</item>
<item>img14</item>
<item>img15</item>
<item>img16</item>
</string-array>
最佳答案
使用此方法:
public class SpinnerRes extends AppCompatActivity implements OnItemSelectedListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final String[] spstr = getResources().getStringArray(R.array.spinnervalue);
final Spinner sp = (Spinner)findViewById(R.id.spinner1);
final ArrayAdapter<String> ar = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,spstr);
sp.setAdapter(ar);
sp.setOnItemSelectedListener(this);
}
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Spinner spinner = (Spinner) parent;
if(spinner.getId() == R.id.spinner1)
{
//do your staff
}
else if(spinner.getId() == R.id.spinner2)
{
//do your staff
}
}
}
关于java - 在Android中设置多个微调器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44109437/