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

前端之家收集整理的这篇文章主要介绍了android – 如何在微调器中添加提示前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

微调器的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定义自定义适配器并禁用第一项,如下所示.

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

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

Add hint in spinner

猜你在找的Android相关文章