简体   繁体   English

如何使Button像Spinner一样

[英]How to make a Button act like a Spinner

I want a Button pull up a menu like a Spinner but it doesn't need to store data like the prompt in a Spinner. 我想要一个按钮拉出像Spinner这样的菜单,但它不需要像Spinner中的提示一样存储数据。

A Spinner looks like this: Spinner看起来像这样:

Spinner spinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.social_list, android.R.layout.simple_gallery_item);//select_dialog_multichoice);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);

spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());

I need to work same thing as a Button... Thanks 我需要像Button一样工作......谢谢

i think you should put spinner.performClick(); 我认为你应该把spinner.performClick(); on button click method 按钮单击方法

You can make use of ContextMenu . 您可以使用ContextMenu Here is a link about Context Menu Demo. 这是一个关于Context Menu Demo的链接。

http://mobile.dzone.com/news/context-menu-android-tutorial http://mobile.dzone.com/news/context-menu-android-tutorial

But little modifications are required. 但是需要很少的修改。 Inside the button click event, you have to open the ContextMenu. 在按钮单击事件中,您必须打开ContextMenu。

just open the dialog with list on button click will be looks same as spinner ..... 只需打开对话框,按钮单击列表就像spinner一样......

as in 如在

http://saga-androidapplication.blogspot.in/2011/05/dialog-list-item.html http://saga-androidapplication.blogspot.in/2011/05/dialog-list-item.html

http://developer.android.com/guide/topics/ui/dialogs.html#AddingAList http://developer.android.com/guide/topics/ui/dialogs.html#AddingAList

use Resources res = getResources(); 使用资源res = getResources();

final String[] items = res.getStringArray(R.array.social_list);
//final CharSequence[] items = {"Red", "Green", "Blue"};

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Pick a color");
    builder.setItems(items, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {
            Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
        }
    });
    AlertDialog alert = builder.create();

Android Custom List Dialog Android自定义列表对话框

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM