项目被选中之前设置微调的文本 [英] Set text of spinner before item is selected

查看:166
本文介绍了项目被选中之前设置微调的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个项目一个微调,我使用XML字符串数组的资源来养活它。当您打开一个活动的微调通常显示的第一个项目是这样的数组列表中显示。我想改变这种状况,并显示在微调文本选择一个,一个项目被选中了。

I have a spinner with three items and I use an XML string-array resource to feed it. When you open an activity the spinner normally shows the first item that's in the array list. I'd like to change that and show the text "Select one" in the spinner, before an item is selected.

我怎么能这样做?

推荐答案

您可以做两种方式之一。

You can do that one of two ways.

1)添加选择一项在你的xml和code中的第一项你的听众忽视,作为一个选择。

1) Add "Select One" as the first item in your xml and code your listener to ignore that as a selection.

2)创建定制适配器以将其插入作为第一行,

2) Create a custom adapter to insert it as the first line,

修改

在你的资源

<string-array name="listarray">
    <item>Select One</item>
    <item>Item One</item>
    <item>Item Two</item>
    <item>Item Three</item>
</string-array>

在你onItemSelected监听器:

In your onItemSelected Listener:

spinnername.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
    public void onNothingSelected(AdapterView<?> parent) {
    }
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
        if (pos == 0) {
        }else {
            // Your code to process the selection
        }
    }
});

这篇关于项目被选中之前设置微调的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆