android– 如何在微调器中添加提示

android– 如何在微调器中添加提示,第1张

概述微调器的XML代码:<Spinner android:id='@+id/mySpinner' https://stackoverflow.com/questions style='@style/Widget.AppCompat.DropDownItem.Spinner' android:layout_wid

微调器的XML代码:

.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中的默认文本.最佳答案没有任何默认方式在微调器中显示提示.
为此,您需要在数组中手动​​添加一个项目,如下所示.

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

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

@OverrIDepublic 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;    }}

您可以更改颜色,如下所示

@OverrIDepublic 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;}

欲了解更多信息,请访问: –

Add hint in spinner 总结

以上是内存溢出为你收集整理的android – 如何在微调器中添加提示全部内容,希望文章能够帮你解决android – 如何在微调器中添加提示所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1139583.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)