如何在微调器中添加提示 [英] How to add a Hint in spinner

查看:64
本文介绍了如何在微调器中添加提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

微调器的XML代码:

<Spinner
    android:id="@+id/mySpinner"
    https://stackoverflow.com/questions       
    style="@style/Widget.AppCompat.DropDownItem.Spinner"
    android:layout_width="match_parent"
    android:layout_height="70dp" />`

.kotlin:

val myStrings = arrayOf("One", "Two" , "Three", "Four")
mySpinner.adapter = ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, myStrings)
mySpinner.onItemSelectedListener = object : 
AdapterView.OnItemSelectedListener {
    override fun onNothingSelected(parent: AdapterView<*>?) {
        TODO("not implemented") 
        //To change body of created functions use File | Settings | File Templates.
    }

    override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
        TODO("not implemented") 
        //To change body of created functions use File | Settings | File Templates.
    }
}}

等于Edittext中的提示"选项,我需要Spinner中的默认文本.

Equal to the "hint" option in Edittext, I need a default text in a Spinner.

推荐答案

没有任何默认方式可以在微调器中显示提示.为此,您需要在下面的数组中手动​​添加一项.

There is not any default way to display hint in spinner. For this you need to add one item manually in the array like below.

val myStrings = arrayOf("Select","One", "Two" , "Three", "Four")

现在,为Spinner定义自定义适配器,并禁用以下第一项.

Now, Define custom Adapter for the Spinner and disable the first item like below.

@Override
public boolean isEnabled(int position) {
    if (position == 0) {
        // Disable the first item from Spinner
        // First item will be use for hint
        return false;
    } else {
        return true;
    }
}

您可以如下更改颜色

@Override
public View getDropDownView(int position, View convertView,
                                    ViewGroup parent) {
    View view = super.getDropDownView(position, convertView, parent);
    TextView tv = (TextView) view;
    if (position == 0) {
        // Set the hint text color gray
        tv.setTextColor(Color.GRAY);
    } else {
        tv.setTextColor(Color.BLACK);
    }
    return view;
}

有关更多信息,请访问:-

For more info please visit:-

在微调器中添加提示

这篇关于如何在微调器中添加提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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