Android微调器设置默认文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20451499/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android spinner set default text
提问by Paldan
I've implemented a spinner into my app. I'm showing a list of schools but I want a default value/prompt so the user knows to select a school. I also want to do this without having to have the prompt as a list item. Any ideas?
我已经在我的应用程序中实现了一个微调器。我正在显示学校列表,但我想要一个默认值/提示,以便用户知道选择一所学校。我也想这样做而不必将提示作为列表项。有任何想法吗?
Here is my spinner code so far:
到目前为止,这是我的微调器代码:
selectSchoolSpinner = (Spinner) findViewById(R.id.select_school_spinner);
spinnerSchoolList = new ArrayList<String>();
spinnerSchoolList.add("Please select School");
//I don't want to have a prompt like the latter
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, spinnerSchoolList);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
selectSchoolSpinner.setAdapter(spinnerAdapter);
回答by Alon Levanon
You should create custom ArrayAdapter with a String for your desired text in its constructor. Then, you should switch the first element of your array with your default text, and after the user click on the spinner it should switch back to the original array.
您应该在其构造函数中为所需文本创建带有字符串的自定义 ArrayAdapter。然后,您应该使用默认文本切换数组的第一个元素,在用户单击微调器后,它应该切换回原始数组。
Example:
例子:
Activity class:
活动类:
public class SpinnerActivity extends Activity {
Spinner spinner;
String defaultTextForSpinner = "Your deafult text here";
String[] arrayForSpinner = {"One", "Two", "Three"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spinner);
spinner = (Spinner) findViewById(R.id.your_spinner);
spinner.setAdapter(new CustomSpinnerAdapter(this, R.layout.spinner_row, arrayForSpinner, defaultTextForSpinner));
}
}
CustomSpinnerAdapter class:
CustomSpinnerAdapter 类:
public class CustomSpinnerAdapter extends ArrayAdapter<String>{
Context context;
String[] objects;
String firstElement;
boolean isFirstTime;
public CustomSpinnerAdapter(Context context, int textViewResourceId, String[] objects, String defaultText) {
super(context, textViewResourceId, objects);
this.context = context;
this.objects = objects;
this.isFirstTime = true;
setDefaultText(defaultText);
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
if(isFirstTime) {
objects[0] = firstElement;
isFirstTime = false;
}
return getCustomView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
notifyDataSetChanged();
return getCustomView(position, convertView, parent);
}
public void setDefaultText(String defaultText) {
this.firstElement = objects[0];
objects[0] = defaultText;
}
public View getCustomView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.spinner_row, parent, false);
TextView label = (TextView) row.findViewById(R.id.spinner_text);
label.setText(objects[position]);
return row;
}
}
回答by Plo_Koon
Try setSelection method to set default text:
尝试 setSelection 方法来设置默认文本:
spinner.setSelection(adapter.getPosition(DEFAULT_CURRENCY_TYPE))
spinner.setSelection(adapter.getPosition(DEFAULT_CURRENCY_TYPE))
And look this link: How to make an Android Spinner with initial text "Select One". It is very useful (@aaronvargas answer is what you need).
并查看此链接:How to make an Android Spinner with initial text "Select One"。它非常有用(@aaronvargas 答案正是您所需要的)。
回答by Tras Ngahu
@Override
protected void onPostExecute(PositionBean[] result) {
String[] spinnerArray = new String[result.length];
if(result!=null) {
for (int i = 0; i < result.length; i++) {
String positionName = result[i].getPositionName();
spinnerArray[i] = result[i].getPositionName();
}
ArrayList<String> positionList = new ArrayList<String>(Arrays.asList(spinnerArray));
positionList.add(0,"select");
String [] positions = positionList.toArray(new String[positionList.size()]);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(EngineActivity.this, android.R.layout.simple_spinner_item,
positions);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
positionSpinner.setAdapter(adapter);
}else{
Log.d("NULL", "This is null");
}
}