我正在使用spinner
,我想知道如何从item
spinner
中image button
出现protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_up);
spinnerListener();
St = (Spinner) findViewById(R.id.Namess);
imageButton01 = (ImageButton) findViewById(R.id.ImageButton1);
}
private void spinnerListener() {
ArrayList<Name> players = List_content.ENTRY_LIST_PLAYERS;
final ArrayAdapter<String> adapter=new ArrayAdapter<String>(Line_up.this, android.R.layout.simple_spinner_item);
{for (Name p : players){adapter.add(p.f_name);}};
playerss.setAdapter(adapter);
playerss.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> av, View v,
int position, long itemId) {
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
}
public void goalkeeper(View v){
st.performClick();
}
图片使用字符串作为值。< / p>
{{1}}
答案 0 :(得分:0)
行。这就是诀窍:
单击ImageButton时,只需要在微调器对象上调用performClick()
。
int[] pictureIds = {
R.drawable.picture1,
//add as many picture IDs as you want
R.drawable.picture2
}
imageButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
spinner.performClick();
}
});
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long arg3) {
imageButton.setImageResource(pictureIds[position]);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
不要忘记设置微调器适配器,就像在问题中正确设置一样。