Android Spinner 提示未显示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23805886/
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
Spinner prompt not showing
提问by anuj
I have a Profession's spinner(drop-down) in which i have list of professions.I want to show the default value as "Select Profession".In my xml i type android:prompt="Select Profession" but nothing is showing up.I wanted "Select Profession" to be shown at the spot where i have marked its as red
我有一个职业的微调器(下拉菜单),其中有职业列表。我想将默认值显示为“选择职业”。在我的 xml 中,我输入 android:prompt="Select Profession" 但没有显示任何内容。我希望在我将其标记为红色的地方显示“选择职业”
Spinner.XML
微调器.XML
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/sp_profession"
android:layout_weight="1"
style="@style/spinner"
android:prompt="Select Profession"
android:spinnerMode="dropdown"
android:layout_margin="2dp"></Spinner>
I did doing something like this but i am getting null value at prompt_text
我确实做了这样的事情,但我在 prompt_text 得到了空值
profession_array = getResources ().getStringArray (R.array.Profession);
profession_str = new ArrayAdapter<String> (c, R.layout.textview_spinner, profession_array);
prompt_text.setText ("Select Profession");
profession_str.setDropDownViewResource (android.R.layout.simple_dropdown_item_1line);
R.layout.textview_spinner
R.layout.textview_spinner
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="@+id/prompt_text"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:minHeight="1dp"
android:gravity="center"
android:textSize="20sp"
android:textColor="@android:color/white" />
回答by Araju
Prompt is used to show title on dropdown popup not for default text.
提示用于在下拉弹出窗口中显示标题而不是默认文本。
I think you are looking for setting the default value on spinner when you have not selected any value from spinner dropdown. For that you need to use NothingSelectedSpinnerAdapter, below is the link for more details :
我认为当您没有从微调器下拉列表中选择任何值时,您正在寻找在微调器上设置默认值。为此,您需要使用 NothingSelectedSpinnerAdapter,以下是更多详细信息的链接:
回答by Hareesh S
Not the correct way but it works.. What ever you wan to show give it as the first item in the String array like this
不是正确的方法,但它有效.. 你想显示什么,把它作为字符串数组中的第一项,像这样
string.xml
字符串.xml
<string-array name="Profession">
<item>Please select the Profession</item>
<item>Student</item>
<item>Prof</item>
<item>staff</item>
<item>research student</item>
in java code when ur reading from spinng object
当您从旋转对象中读取时,在 Java 代码中
Spinner profession = (Spinner)findViewById(R.id.profession);
String prof = String.valueOf(profession.getSelectedItem());
if(prof.equals("Please select the Profession"))
{
Toast.makeText(getApplicationContext(), "Please select the Profession", Toast.LENGTH_SHORT).show();
}else{
//Do your thing here....
}
回答by Gordon
you should set style ---> style="@android:style/Widget.Spinner" works for me. Hope it help.
你应该设置 style ---> style="@android:style/Widget.Spinner" 对我有用。希望有帮助。
回答by John Schmitt
I had the same problem and style="@android:style/Widget.Spinner"
was the solution for me as well. just insert into the Spinner tag without the android: preface
我有同样的问题,style="@android:style/Widget.Spinner"
也是我的解决方案。只需插入不带 android: preface 的 Spinner 标签