隐藏Android下拉框的文本

4

我有一个带有背景图的旋转器(spinner)。但是当我向旋转器添加数组适配器时,文字会显示在背景图上。我希望隐藏这个文字。同时,我还有一个TextView。我的需求是,当我点击图片时,旋转器应该弹出;当我选择一个项目后,TextView会更新为所选项目。


重复问题但没有答案:http://stackoverflow.com/questions/4342273/how-do-i-hide-the-text-on-a-spinner - Harry Joy
我遇到了同样的问题,但我发现我的9 patch png文件无效,因此修复9 patch并清理-构建项目对我有用。您能否发布您的代码,以便我们更容易地为您提供建议。 - Hiral Vadodaria
请查看以下链接,其中提供了从Spinner中删除文本的解决方案:http://stackoverflow.com/questions/15479579/remove-text-from-spinner - Yuriy Vasylenko
3个回答

15
Spinner spin = (Spinner) d.findViewById(R.id.searchCriteria);  
spin.setOnItemSelectedListener(new OnItemSelectedListener() {  
  public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {  
// hide selection text  
((TextView)view).setText(null);  
// if you want you can change background here  
}  
  public void onNothingSelected(AdapterView<?> arg0) {}  
 });

2
在您的布局文件夹中创建ghost_text.xml,并使用以下文本:
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:textColor="@android:color/transparent" />

然后在你的适配器中像这样使用该文件:
    ArrayAdapter<String> your_adapter = new ArrayAdapter<String>(this, 
        R.layout.ghost_text, your_list);
    your_spinner.setAdapter(your_adapter);

你还应该能够使用这种技术来处理其他基于资源的适配器。

0

你可以定义一个 XML 布局文件,其中不包含任何 TextView,并将其提供给 Spinner 的适配器。

例如:empty_spinner_item.xml

<ImageView xmlns:android="http..."
    android:layout_width=".." 
/>

然后使用任何适配器:

spinner.setAdapter(new SimpleAdapter(this, data, R.layout.empty_spinner_item, ... ));

我有一个带有背景图像的Spinner。但是当我向Spinner添加数组适配器时,文本会显示在背景图像上。我想隐藏这个文本。还有一个TextView。我的要求是,当我点击图像时,Spinner应该弹出,当我选择一个项目时,TextView将更新为所选项目。 - indira
1
我无法隐藏Spinner的文本。所以我用一个按钮替换了Spinner。当按钮被点击时,Spinner弹出(spinner.performClick)。我还编写了一个OnItemSelectedListener来更新TextView中的选定项目。但是OnItemSelectedListener不起作用:(。请帮忙解决一下。 - indira
1
它能运行。StorageSpinner.setOnItemSelectedListener(new OnItemSelectedListener() { public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { tv.setText(StorageSpinner.getSelectedItem()+""); } public void onNothingSelected(AdapterView arg0) {}}); - indira
当我尝试这样做时,我的应用程序崩溃并显示错误信息:“ArrayAdapter 要求资源 ID 是 TextView 类型。” - arlomedia

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接