问题
In an Android application, is it possible to open the spinner popup from a button click instead of pressing the actual spinner?
I have tried the following:
Button btnChange = (Button)findViewById(R.id.btnChange);
btnChange.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Spinner mySpinner = (Spinner) findViewById(R.id.sSpinner);
mySpinner.showContextMenu();
}
});
回答1:
Sorry for late answer - it's possible:
((Spinner) findViewById(R.id.mySpinner)).performClick();
回答2:
It is possible, you just call it popup menu, not spinner.
ImageView imageView = (ImageView) findViewById(R.id.image);
imageView .setOnClickListener(new OnClickListener(){
PopupMenu pum = new PopupMenu(this, findViewById(R.id.image));
pum.inflate(R.menu.image_chooser_popup);
pum.show();
});
}
Your spinner (or popup) items goes to R.menu.image_chooser_popup:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:title="take a picture"
android:titleCondensed="camera"
android:visible="true"
android:onClick="cameraIntent" />
<item
android:title="choose picture from gallery"
android:titleCondensed="string"
android:visible="true"
android:onClick="galleryIntent"/>
Hope this one helps some one. If you have any issues with my responce, please fill free to ask.
回答3:
Try this one:
Spinner mySpinner = (Spinner) findViewById(R.id.sSpinner);
Button btnChange = (Button)findViewById(R.id.btnChange);
btnChange.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mySpinner.performClick();
}
});
来源:https://stackoverflow.com/questions/4838992/android-open-spinner-from-button