Android: Open Spinner from Button

时光怂恿深爱的人放手 提交于 2019-12-04 10:49:42

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!