我的项目中有2个微调器,到目前为止只有一个微调器工作!我正在尝试使用2个微调器,当选择更改图像视图中的图像!第一个微调器从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>
答案 0 :(得分:2)
使用这种方法:
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
}
}
}
答案 1 :(得分:1)
请查看此代码根据您做的更改,
public class SpinnerRes extends Activity {
ImageView im;
Spinner sp,sp2;
String[] spstr;
String[] spstr2;
ArrayAdapter<String> ar;
ArrayAdapter<String> ar2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.b);
im = (ImageView)findViewById(R.id.imageView1);
spstr = getResources().getStringArray(R.array.spinnervalue);
spstr2 = getResources().getStringArray(R.array.spinnervalue2);
sp = (Spinner)findViewById(R.id.spinner1);
sp2 = (Spinner)findViewById(R.id.spinner2);
ar = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,spstr);
ar2 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,spstr2);
sp.setAdapter(ar);
sp2.setAdapter(ar2);
sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
String text = sp.getSelectedItem().toString();
if(text.equals("A"))
im.setImageDrawable(getResources().getDrawable(R.mipmap.ic_launcher));
if(text.equals("A6"))
im.setImageDrawable(getResources().getDrawable(R.drawable.built_in_active));
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
sp2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
String text = sp2.getSelectedItem().toString();
if(text.equals("A"))
im.setImageDrawable(getResources().getDrawable(R.mipmap.ic_launcher));
if(text.equals("A6"))
im.setImageDrawable(getResources().getDrawable(R.drawable.built_in_active));
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
}