微调器的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 中的默认文本。
最佳答案
在微调器中显示提示没有任何默认方式。 为此,您需要在数组中手动添加一项,如下所示。
val myStrings = arrayOf("Select","One", "Two" , "Three", "Four")
现在, 为 Spinner 定义自定义适配器并禁用第一项,如下所示。
@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;
}
更多信息请访问:-
关于android - 如何在微调器中添加提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49508512/