如何将微调器中的第一个值(默认值)设置为空
Posted
技术标签:
【中文标题】如何将微调器中的第一个值(默认值)设置为空【英文标题】:How to set my first value (default value) in spinner as empty 【发布时间】:2015-12-11 15:51:41 【问题描述】:。意思是,在用户从列表中选择值之前,微调器将没有任何值。我在下面尝试了
list.add("");
但是,它不会起作用。下面是我的代码:
if(values.contains("options"))
spinner.setOnItemSelectedListener(MyClass.this);
List<String> list = new ArrayList<String>();
list.add("");
list.add("Test");
list.add("Working");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_spinner_item, list) ;
adapter.setDropDownViewResource(R.layout.custom_spinner_list);
spinner.setAdapter(adapter);
【问题讨论】:
empty 表示你要设置什么 @user3676184 - 空表示“空”值或“无”字符串 No Rustam,我不想清除我的列表,我只想让 spinner 的默认值为空,这意味着只有在用户选择后才会显示值,直到那时 spinner 将没有任何价值 你试过 spinner.setPrompt("");或将其放入您的 xml (Spinner) android:prompt="" 微调器当前默认显示什么? 【参考方案1】:我找到了方法。下面是我们的做法,让最后一行是
list.add("");
并添加以下代码:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_spinner_item, list)
@Override
public View getView(int position, View convertView, ViewGroup parent)
View v = super.getView(position, convertView, parent);
if (position == getCount())
((TextView)v.findViewById(android.R.id.text1)).setText("");
((TextView)v.findViewById(android.R.id.text1)).setHint(getItem(getCount())); //"Hint to be displayed"
return v;
@Override
public int getCount()
return super.getCount()-1; // you don't display last item. It is used as hint.
;
adapter.setDropDownViewResource(R.layout.custom_spinner_list);
spinner.setAdapter(adapter);
spinner.setSelection(adapter.getCount()); //set the hint the default selection so it appears on launch.
spinner.setOnItemSelectedListener(this);
notifyDataSetChanged();
【讨论】:
这是一个了不起的解决方案,因为它避免了不得不破解第一个视图而不会出现。我在下面发布了一个 SpinnerNoDefault 类,它考虑了这个答案。谢谢!【参考方案2】: List<String> allGoverNames = new ArrayList<String>();
allGoverNames.add(0, "");
allGoverNames.add(1,"Cricket");
allGoverNames.add(2,"FootBall");
allGoverNames.add(3,"Chess");
ArrayAdapter<String> GoverdataAdapter = new ArrayAdapter<String>
(context, android.R.layout.simple_spinner_item, allGoverNames);
GoverdataAdapter.setDropDownViewResource
(android.R.layout.simple_spinner_dropdown_item);
GovernorateSpinner.setAdapter(GoverdataAdapter);
【讨论】:
【参考方案3】:在 SearchableSpinner (com.toptoche.searchablespinnerlibrary.SearchableSpinner) 中有一个名为“app:hintText”的参数 - 您可以将其用于您的目的。例如:
<com.toptoche.searchablespinnerlibrary.SearchableSpinner
android:id="@+id/vehicle_name"
android:layout_
android:layout_
android:background="@drawable/spinner_background"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="20dp"
app:hintText=" "/>
一旦从 Spinner 列表中选择了某些内容,提示就会消失。
【讨论】:
【参考方案4】:感谢上面user45678的回答,我写了这个
public class SpinnerNoDefault extends Spinner
public void initialize(List<String> items)
initialize(items, android.R.layout.simple_spinner_item, android.R.layout.simple_spinner_dropdown_item);
public void initialize(List<String> items, @LayoutRes int mItemResId, @LayoutRes int dropdownResId)
List<String> itemsWithEmpty = new ArrayList<>(items);
itemsWithEmpty.add("");
String[] choicesArray = itemsWithEmpty.toArray(new String[itemsWithEmpty.size()]);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(getContext(), mItemResId, choicesArray)
@NonNull
@Override
public View getView(int position, View convertView, @NonNull ViewGroup parent)
View v = super.getView(position, convertView, parent);
if (position == getCount())
((TextView)v.findViewById(android.R.id.text1)).setText(null);
((TextView)v.findViewById(android.R.id.text1)).setHint(parent.getContext().getString(R.string.spinner_no_default_hint));
return v;
@Override
public int getCount()
// Don't display last item. It is used as hint.
return super.getCount()-1;
;
spinnerArrayAdapter.setDropDownViewResource(dropdownResId);
setAdapter(spinnerArrayAdapter);
setSelection(itemsWithEmpty.size()-1);
【讨论】:
【参考方案5】:起初。当您初始化微调器时添加
spinner.setSelection(0, false)
我用选定的项目创建了这个适配器:
abstract class SpinnerSelectAdapter<T>(
context: Context,
private val resource: Int,
@ColorRes private val unselectedColorResource: Int,
@ColorRes private val selectedColorResource: Int,
open var objects: List<T>
) :
ArrayAdapter<T>(context, resource, objects)
var selectedPosition = -1
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View
val view = convertView ?: LayoutInflater.from(parent.context)
.inflate(resource, parent, false)
if(selectedPosition != -1)
setView(view, getItem(position)!!)
return view
override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup): View
val view = convertView ?: LayoutInflater.from(parent.context)
.inflate(resource, parent, false)
if(selectedPosition == position)
view.setBackgroundResource(selectedColorResource)
else
view.setBackgroundResource(unselectedColorResource)
setView(view, getItem(position)!!)
return view
open fun setView(view: View, item: T)
open fun setEmptyView(view: View)
override fun getItem(position: Int): T?
return objects[position]
override fun getCount(): Int
return objects.size
然后我为从 SpinnerSelectAdapter 继承的字符串值创建了适配器
class SpinnerStringAdapter(
context: Context,
resource: Int,
@ColorRes unselectedColorResource: Int,
@ColorRes selectedColorResource: Int,
override var objects: List<String>
) :
SpinnerSelectAdapter<String>(
context,
resource,
unselectedColorResource,
selectedColorResource,
objects
)
override fun setView(view: View, item: String)
val txt = view.findViewById<TextView>(android.R.id.text1)
txt.text = item
然后在您的片段或活动中
val adapter = SpinnerStringAdapter(
requireContext(),
R.layout.spinner_item,
R.color.colorBG,
R.color.colorSubBG,
types
)
spinner.adapter = typeAdapter
spinner.setSelection(0, false)
spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener
override fun onItemSelected(
parent: AdapterView<*>?,
view: View?,
position: Int,
id: Long
)
adapter.selectedPosition = position
adapter.notifyDataSetChanged()
``
【讨论】:
以上是关于如何将微调器中的第一个值(默认值)设置为空的主要内容,如果未能解决你的问题,请参考以下文章
如何将 Angular 日期范围选择器中的日期设置为该月的第一天