【问题标题】:setPrompt on Spinner doesn't work [duplicate]微调器上的 setPrompt 不起作用[重复]
【发布时间】:2012-11-10 22:13:19
【问题描述】:

我对 android 开发非常陌生,我似乎有一个我无法理解的问题。我有一个微调器,值设置得很好,但我的提示没有出现。我试图在 xml 和 java 代码中设置它,但两者都不起作用。下面是我的配置。我觉得我缺少一些基本的东西,但我还不知道它是什么。

任何帮助将不胜感激。克雷格

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#000000" >

    <Spinner
        android:id="@+id/band"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_marginTop="72dp"
        android:prompt="@string/year_prompt"
        android:textColor="#F0F0F0" />

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="28dp"
        android:entries="@array/year_array"
        android:prompt="@string/year_prompt"
        android:textColor="#F0F0F0" />

</RelativeLayout>




<string name="year_prompt">Select A Year</string>
<string-array name="year_array">
    <item >2000</item>
    <item >2001</item>
    <item >2002</item>
    <item >2003</item>
    <item >2004</item>
    <item >2005</item>
</string-array>

【问题讨论】:

  • 你用的是什么版本的安卓?在 android 3.0+ 中我没有注意到提示被显示了
  • 我目前使用的是 4.1.2。这是否意味着不推荐使用提示功能?有没有更好的方法来完成同样的事情。

标签: android spinner


【解决方案1】:

你的promotion没有显示是因为在android 3.0+中,没有提示空间。

这是 3.0 之前的微调器的屏幕截图

提示在顶部,上面写着“选择一个国家”,屏幕截图来自http://www.mkyong.com/android/android-spinner-drop-down-list-example/

所以提示不再显示。

有几种可能性。

首先,您可以在 list-array 的顶部添加一个项目,与您的提示内容相同

否则,您可以使用一个按钮并从中创建一个下拉列表。

首先,在你的布局中用一个按钮替换你的微调器,并给它你提供提示的文本

然后将您的微调器上的项目列表移动到名为 optionmenu.xml 的文件中

然后声明您的按钮并为其设置onClickListener。

在该方法中,输入以下代码

在第四行,您指定的布局是您的选项菜单布局

public void showPopup(View v) {
PopupMenu popup = new PopupMenu(this, v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.layout.optionmenu, popup.getMenu());
popup.show();

} 调用该方法时,需要进行如下操作

    View p = (View)findViewById(R.id.spinner button);
showPopup(p);

在此代码中,View p 是您要显示菜单上角位置的视图。所以在这种情况下,它将是您的按钮

在此处查看答案以实现它 - android menu code not working

在每个case:..... 语句中,放置一行将按钮文本设置为字符串。

case R.id.item1:

button.setText("item1");>

这里还有一些其他好的解决方案 -

How to make an Android Spinner with initial text "Select One"

【讨论】:

  • 非常感谢。这是非常有用的信息和帮助。我将发布我的最终解决方案。克雷格
  • FWIW - 我尝试了“按钮方法”并且效果很好。功能是完美的,但我真的没有挖掘太多的 UI(太多的事情发生了)。然后我决定使用我的列表数组的第一个值来获得值“请选择...”。我不喜欢在我的微调器中有“假”值,然后必须围绕它们进行编码(例如点击假值),但它似乎工作正常并且 UI 更干净。谢谢大家。
  • 您正在寻找的可以使用这个stackoverflow.com/a/14546126/452487
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多