微调器提示没有显示

时间:2014-05-22 11:44:19

标签: android spinner

enter image description here我有一个专业的微调器(下拉列表),其中我有专业列表。我想将默认值显示为“选择职业”。在我的xml中我输入android:prompt =“选择职业“但没有任何东西出现。我希望”选择职业“在我标记为红色的地方展示

Spinner.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> 

我做过类似的事情,但我在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

<?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" />

4 个答案:

答案 0 :(得分:11)

提示用于在下拉弹出窗口中显示标题,而不是默认文本。

我认为您在spinner下拉列表中未选择任何值时,正在寻找在微调器上设置默认值。为此您需要使用NothingSelectedSpinnerAdapter,下面是链接以获取更多详细信息:

https://stackoverflow.com/a/12221309/2389804

答案 1 :(得分:6)

不是正确的方法,但它的工作原理..你想要展示的是什么,将它作为String数组中的第一项,如下所示

string.xml

 <string-array name="Profession">
    <item>Please select the Profession</item>
    <item>Student</item>
    <item>Prof</item>
    <item>staff</item>
    <item>research student</item>

当你从spinng对象中读取

时,在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....
    }

答案 2 :(得分:5)

你应该设置风格---&gt; 风格=“@安卓风格/ Widget.Spinner” 适合我。 希望它有所帮助。

答案 3 :(得分:0)

我遇到了同样的问题,style="@android:style/Widget.Spinner"也是我的解决方案。只需在没有android:preface

的情况下插入Spinner标签
相关问题