Java 弹出 android 选择列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6406948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 05:41:29  来源:igfitidea点击:

pop up select list for android

javaandroid

提问by Denoteone

I am trying to get away from using 5 buttons when I would like to have one when pressed pops up a window that allows the user to select where they want to go.

当我想在按下时弹出一个窗口,让用户选择他们想去的地方时,我试图避免使用 5 个按钮。

Example: button "Country"

示例:按钮“国家”

 <Button
android:id="@+id/countrySelect"
android:layout_width="300px"
android:layout_height="wrap_content"
android:text="@string/backhome"
android:layout_x="8px"
android:layout_y="21px"
>
</Button>

and when pressed it would pop up a list of countrys to select from: Something like-

当按下它时,它会弹出一个可供选择的国家/地区列表:类似-

countrySelect.setOnClickListener(new OnClickListener() {         
    @Override
    public void onClick(View view){

           //POP UP SELECT MENU WHEN SELECTED START A NEW INTENT
           Intent myIntent = new Intent(view.getContext(), ***SELECT MENU CONTROLS***.class);
           startActivityForResult(myIntent, 0);

        }

 });

Sorry this probably is easy fix but I am not having much luck when I researched it.

抱歉,这可能很容易解决,但我在研究它时运气不佳。

采纳答案by Tushar

You can add spinnerin your layout.xml :

您可以在 layout.xml 中添加微调器

<Spinner 
android:id="@+id/areaspinner"
android:layout_width="150dip"
android:layout_height="40dip"
android:drawSelectorOnTop="true"
android:padding="5dip"
android:paddingLeft="10dip"/>  

Now in Activity.java :

现在在 Activity.java 中:

areaspinner = (Spinner) findViewById(R.id.areaspinner);
    ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, array);  //array you are populating  
    adapter2.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
    areaspinner.setAdapter(adapter2);
        areaspinner.setSelection(Integer.parseInt(strarea));  

Now you can get gtghe selected value from the spinner by :

现在您可以通过以下方式从微调器中获取 gtghe 选定值:

int ipos=areaspinner.getSelectedItemPosition();
String str=array[iPos];  

Good luck.

祝你好运。

回答by denis.solonenko

You could use AlertDialogfor that. It can show a list of items and react when user taps on any of them http://developer.android.com/guide/topics/ui/dialogs.html#AlertDialog

你可以用AlertDialog它。它可以显示项目列表并在用户点击其中任何一个时做出反应http://developer.android.com/guide/topics/ui/dialogs.html#AlertDialog

回答by Tushar

Why don't you use Spinner or Single choice alert dialog for the same. http://developer.android.com/guide/topics/ui/dialogs.html#AlertDialog

为什么不使用 Spinner 或 Single selection 警报对话框呢? http://developer.android.com/guide/topics/ui/dialogs.html#AlertDialog

回答by Asaf Pinhassi

If you want a popup menu like a spinner but with a button, you can use PopupMenu

如果你想要一个像微调器一样的弹出菜单但有一个按钮,你可以使用PopupMenu

see thisexample

这个例子