我有一个微调器有一些价值观
| Monday | | Thuesday | | Wednesday | | Thursday | | Friday | | Saturday | | USER DEFINED |
当用户选择USER DEFINED时,他可以在对话框中输入自定义值,假设我将该值设为String userDef =“Your choice”.
我需要将此String设置为当前项,而不更改微调选择列表,必须与上述相同,当用户再次点击微调器时,如Google Analytics(分析)Android应用程序中的内容,请参阅该图像.
未点击的微调
点击微调
我该怎么做?
解决方法
实现这一点的关键细节是Spinner使用的
SpinnerAdapter
界面有两种不同但相关的方法:
> getView()
– 创建Spinner本身显示的视图.
> getDropDownView()
– 创建下拉弹出窗口中显示的视图.
因此,要使一个在弹出窗口本身不同弹出窗口中显示的项目,您只需要以不同的方式实现这两种方法.根据您的代码的细节,细节可能会有所不同,但一个简单的例子就是:
public class AdapterWithCustomItem extends ArrayAdapter<String> { private final static int POSITION_USER_DEFINED = 6; private final static String[] OPTIONS = new String[] { "Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Custom..." }; private String mCustomText = ""; public AdapterWithCustomItem(Context context){ super(context,android.R.layout.simple_spinner_dropdown_item,OPTIONS); } @Override public View getView(int position,View convertView,ViewGroup parent) { View view = super.getView(position,convertView,parent); if (position == POSITION_USER_DEFINED) { TextView tv = (TextView)view.findViewById(android.R.id.text1); tv.setText(mCustomText); } return view; } public void setCustomText(String customText) { // Call to set the text that must be shown in the spinner for the custom option. mCustomText = customText; notifyDataSetChanged(); } @Override public View getDropDownView(int position,ViewGroup parent) { // No need for this override,actually. It's just to clarify the difference. return super.getDropDownView(position,parent); } }
然后,当用户输入自定义值时,您只需要使用要显示的文本调用适配器上的setCustomText()方法即可.
mAdapter.setCustomText("This is displayed for the custom option");
产生以下结果: