Android微调器高亮显示所选项目 [英] Android spinner highlight selected item
问题描述
我有一个基本的android微调器,单击它后,我想得到一个列表,其中包含突出显示的一项,即最初选定的一项.
I've got basic android spinner and I would like to have, after clicking it, the list with items with one of them highlighted, the one, that was originally selected.
就像在这里完成的那样: http://www.warriorpoint.com/blog/wp-content/uploads/2009/05/05spinner-thumb.png
Like it's done here: http://www.warriorpoint.com/blog/wp-content/uploads/2009/05/05spinner-thumb.png
但是使用我自己的项目布局,而不是使用单选框,而是使用我自己的背景.
But with my own layout of the items and not with the radio box, but with my own background instead.
我该如何实现?选择器中是否有任何用处,还是我必须通过编程来完成?
How can I achieve this? Is there anything of use in the selector, or do I have to do it programatically?
感谢您的帮助.
推荐答案
以下是我测试并验证的解决方案.您可以使用setSelection(N)突出显示所需的项目.
Below is an solution I tested and verified. You can use setSelection(N) to highlight the item you want.
class HighLightArrayAdapter extends ArrayAdapter<CharSequence> {
private int mSelectedIndex = -1;
public void setSelection(int position) {
mSelectedIndex = position;
notifyDataSetChanged();
}
public HighLightArrayAdapter(Context context, int resource, CharSequence[] objects) {
super(context, resource, objects);
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View itemView = super.getDropDownView(position, convertView, parent);
if (position == mSelectedIndex) {
itemView.setBackgroundColor(Color.rgb(56,184,226));
} else {
itemView.setBackgroundColor(Color.TRANSPARENT);
}
return itemView;
}
}
这篇关于Android微调器高亮显示所选项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!