Android spinner prompt
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26543408/
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
Android spinner prompt
提问by atapi19
I have a problem with android:prompt
for a spinner. I used this code in the XML file but it doesn't work:
I have a problem with android:prompt
for a spinner. I used this code in the XML file but it doesn't work:
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="120dp"
android:prompt="@string/club_type">
</Spinner>
I also tried to use this code in my main activity but this doesn't work either:
I also tried to use this code in my main activity but this doesn't work either:
spinner.setPrompt("Select club");
While I was using the second case I didn't use android:prompt
; in other words, I tried them individually. Could someone help me?
While I was using the second case I didn't use android:prompt
; in other words, I tried them individually. Could someone help me?
采纳答案by Nabin
Working perfectly on mine.
Working perfectly on mine.
You are mistaking promptwith first element. Tap on the spinner and you will see Select club
as the heading which is the prompt.
You are mistaking promptwith first element. Tap on the spinner and you will see Select club
as the heading which is the prompt.
Hope this helps.
Hope this helps.
回答by RobotCharlie
There are two ways you can deal with that:
There are two ways you can deal with that:
Static way:
Static way:
add one line code in XML's Spinner tag
add one line code in XML's Spinner tag
android:spinnerMode="dialog"
and then set:
and then set:
android:prompt="PROMPT"
In dynamic way:
In dynamic way:
use
use
Spinner spinner = (Spinner)findViewById(R.id.spnner);
String[] myItems= getResources().getStringArray(R.array.spinner1);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this,
android.R.layout.select_dialog_item, myItems);
spinner.setPrompt("PROMPT");
when you set and initialize your adapter
when you set and initialize your adapter
hope that can help you! :)
hope that can help you! :)