Android spinner prompt

早过忘川 提交于 2020-01-01 04:17:32

问题


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:

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?


回答1:


Working perfectly on mine.

You are mistaking prompt with first element. Tap on the spinner and you will see Select club as the heading which is the prompt.

Hope this helps.




回答2:


There are two ways you can deal with that:

Static way:

add one line code in XML's Spinner tag

android:spinnerMode="dialog"

and then set:

android:prompt="PROMPT"

In dynamic way:

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

hope that can help you! :)



来源:https://stackoverflow.com/questions/26543408/android-spinner-prompt

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